I seem to be having trouble getting to grips with implementing a simple JTable that when a button is pressed on a GUI it simply updates the contents of the table
So far I seem to be getting mixed ideas of what type of table to implement to do this but from searching around it seems to be DefaultTable model.
So far I have declared the table
private JTable table;
static Object[] columnNames = new Object[]{"Column 1","Column 2"};
static Object[][] rowData = { {"1", "2"} };
public TestTables() {
DefaultTableModel tableModel;
tableModel = new DefaultTableModel(rowData, columnNames);
}
I’m not even sure if I have declared this correctly, but when I run the GUI it shows the table data correctly. I just dont understand how I can update it using an action performed
public void actionPerformed(ActionEvent e) {
if (e.getSource() == X1btn) {
// not sure how to set table to be {"3", "4"} instead
}
In your actionPerformed method, get a reference to the JTable’s model (the details of this will depend on your program’s structure — something we don’t know at present) which will be a DefaultTableModel, and then you can easily do whatever needs to be done with this object including adding rows, removing rows, changing values held by cells…
If you’re still stuck, consider creating and posting a minimal compilable and runnable example that demonstrates your problem, an sscce.
For example: