import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Main
extends JFrame {
public Main() {
setTitle("Medicine Table");
setSize(400, 300);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
// Create the table model with columns "Medicine" and "Amount"
model.addColumn("Medicine");
model.addColumn("Amount");
// Create the table using the model
// Create scroll pane to hold the table
// Create text fields for user input
// Create labels for text fields
// Create button to add data to the table
@Override
addData();
}
});
// Create panel to hold input components
inputPanel.add(medicineLabel);
inputPanel.add(medicineField);
inputPanel.add(amountLabel);
inputPanel.add(amountField);
inputPanel.add(addButton);
// Add components to the frame
}
// Method to add data to the table
private void addData() {
String medicine
= medicineField.
getText(); String amount
= amountField.
getText();
// Check if both fields are not empty
if (!medicine.isEmpty() && !amount.isEmpty()) {
// Add data to the table
model.
addRow(new String[]{medicine, amount
});
// Clear input fields
medicineField.setText("");
amountField.setText("");
} else {
JOptionPane.
showMessageDialog(this,
"Please enter both medicine and amount."); }
}
public static void main
(String[] args
) { @Override
public void run() {
new Main().setVisible(true);
}
});
}
}