Shashwat Black
Shashwat Black

Reputation: 1002

Dynamically updating jTable

My table will change the entire dataset during runtime. My current code looks like below,

public class gui_test_form extends JFrame{
    private JPanel rootpanel;
    private JTable testTable;
    private JScrollPane testScrollPane;
    private JButton testButton;

    private String[] columnNames = {"Name", "Color"};
    private Object[][] data = { {"John", "Blue"}, {"Oliver", "Green"}, {"Paul", "Red"} };

    public gui_test_form() {
        super("GUI TEST");
        setContentPane(rootpanel);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
        setVisible(true);

        testButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) { // Button Clicked
                //Get new values
                data[0][0] = "New Value";
                //Update table
                testTable.setModel(new DefaultTableModel(data, columnNames));
            }
        });
    }
}

The code works as I expected. But I don't think making a new DefaultTableModel everytime is the best way to go. What should I be doing?

I looked briefly into fireTableChanged() method for AbstractTableModel, but couldn't make it work. I expected it would work since DefaultTableModel is inherited from AbstractTableModel.

Any help is appreciated.

-------Edit-------
Forgive me if I wasn't clearer before, but the problem is that I want to update the whole dataset. Even the Column names and the number of columns and rows are going to change.
For example, in the above code I could do this, and it would still work as you'd expect.

//Get new values
columnNames = new String[]{"Country", "Location", "Latitudes"};
data = new Object[][]{ {"John", "Blue", "1"}, {"Oliver", "Green", "4"}};
//Update table

Upvotes: 2

Views: 10833

Answers (2)

ParisaN
ParisaN

Reputation: 2092

Add below code in wherever you want to update the model.

model.addRow(new Object[]{txt_title.getText(), txt_name.getText()});

Before, The model and table must also be predefined globally.

DefaultTableModel model = new DefaultTableModel();

Upvotes: 0

MarsAtomic
MarsAtomic

Reputation: 10696

You should be declaring a single table model at the same level where you create your JTable and making changes to that table model as required, rather than declaring it in the event handler.

private JPanel rootpanel;
private JTable testTable;
private DefaultTableModel tableModel;
private JScrollPane testScrollPane;
private JButton testButton;

tableModel = new DefaultTableModel();
testTable = new JTable(tableModel);

Take a look at the Java Tutorial. Pay attention to the section on listening for table data changes

If you come up with your own table model, you need to be sure to fully implement all overridden methods, particularly setValueAt(), and make sure you keep track of your row count properly. One of the most common mistakes involves forgetting to increment your row count after adding data and thinking that the table model is not receiving data.

Upvotes: 2

Related Questions