How to add a new row to JXTable at runtime?
Question
I am using JXTable to display a list of records.
When I click "Refresh" button, I want the JXTable to be refreshed to display the newly inserted records.
And also when I click "Add new Row" button, a new row must be added to JXTable.
How can I do these? I am unable to find any useful reference or samples. Please guide me.
Solution
It works the same as with a regular JTable
, which is all explained in the table tutorial. But to answer your questions:
- There is no need for a 'Refresh' button, unless the 'Refresh' button updates the
TableModel
. The moment you update yourTableModel
you should fire the appropriate events so that theJTable
is aware of the changes made to the model. At that moment, theJTable
will refresh itself automatically. If yourTableModel
extends fromDefaultTableModel
there are already methods available which take care of the events (like e.g.insertRow
) - Inserting a row means adding a row to the
TableModel
+ firing the appropriate events. If you use aDefaultTableModel
, you can use the available API of theDefaultTableModel
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow