fork download
  1. import javax.swing.*;
  2. import javax.swing.table.DefaultTableModel;
  3. import java.awt.*;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6.  
  7. public class Main extends JFrame {
  8. private DefaultTableModel model;
  9. private JTable table;
  10. private JTextField medicineField;
  11. private JTextField amountField;
  12.  
  13. public Main() {
  14. setTitle("Medicine Table");
  15. setSize(400, 300);
  16. setDefaultCloseOperation(EXIT_ON_CLOSE);
  17. setLocationRelativeTo(null);
  18.  
  19. // Create the table model with columns "Medicine" and "Amount"
  20. model = new DefaultTableModel();
  21. model.addColumn("Medicine");
  22. model.addColumn("Amount");
  23.  
  24. // Create the table using the model
  25. table = new JTable(model);
  26.  
  27. // Create scroll pane to hold the table
  28. JScrollPane scrollPane = new JScrollPane(table);
  29.  
  30. // Create text fields for user input
  31. medicineField = new JTextField(15);
  32. amountField = new JTextField(5);
  33.  
  34. // Create labels for text fields
  35. JLabel medicineLabel = new JLabel("Medicine:");
  36. JLabel amountLabel = new JLabel("Amount:");
  37.  
  38. // Create button to add data to the table
  39. JButton addButton = new JButton("Add");
  40. addButton.addActionListener(new ActionListener() {
  41. @Override
  42. public void actionPerformed(ActionEvent e) {
  43. addData();
  44. }
  45. });
  46.  
  47. // Create panel to hold input components
  48. JPanel inputPanel = new JPanel();
  49. inputPanel.add(medicineLabel);
  50. inputPanel.add(medicineField);
  51. inputPanel.add(amountLabel);
  52. inputPanel.add(amountField);
  53. inputPanel.add(addButton);
  54.  
  55. // Add components to the frame
  56. getContentPane().setLayout(new BorderLayout());
  57. getContentPane().add(scrollPane, BorderLayout.CENTER);
  58. getContentPane().add(inputPanel, BorderLayout.SOUTH);
  59. }
  60.  
  61. // Method to add data to the table
  62. private void addData() {
  63. String medicine = medicineField.getText();
  64. String amount = amountField.getText();
  65.  
  66. // Check if both fields are not empty
  67. if (!medicine.isEmpty() && !amount.isEmpty()) {
  68. // Add data to the table
  69. model.addRow(new String[]{medicine, amount});
  70.  
  71. // Clear input fields
  72. medicineField.setText("");
  73. amountField.setText("");
  74. } else {
  75. JOptionPane.showMessageDialog(this, "Please enter both medicine and amount.");
  76. }
  77. }
  78.  
  79. public static void main(String[] args) {
  80. SwingUtilities.invokeLater(new Runnable() {
  81. @Override
  82. public void run() {
  83. new Main().setVisible(true);
  84. }
  85. });
  86. }
  87. }
  88.  
Success #stdin #stdout #stderr 0.32s 67432KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Exception in thread "AWT-EventQueue-0" java.awt.HeadlessException: 
No X11 DISPLAY variable was set, but this program performed an operation which requires it.
	at java.desktop/java.awt.GraphicsEnvironment.checkHeadless(GraphicsEnvironment.java:197)
	at java.desktop/java.awt.Window.<init>(Window.java:538)
	at java.desktop/java.awt.Frame.<init>(Frame.java:423)
	at java.desktop/java.awt.Frame.<init>(Frame.java:388)
	at java.desktop/javax.swing.JFrame.<init>(JFrame.java:180)
	at Main.<init>(Main.java:13)
	at Main$2.run(Main.java:83)
	at java.desktop/java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:313)
	at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:770)
	at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721)
	at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:715)
	at java.base/java.security.AccessController.doPrivileged(AccessController.java:389)
	at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
	at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:740)
	at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
	at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
	at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
	at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
	at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
	at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)