Reputation: 3426
I am a newbie to java and trying to refresh the jTable. What i am trying to do is the list of data in Jtable is displayed now i do have a refresh icon on which if i click, it refreshes the table and updates the records in it. i have tried lot many things but not succeeded. What all i have tried is:
// took ref. help from this:
try 1:
private void remakeData(CollectionType< Objects > name) {
model.setRowCount(0);
for (CollectionType Objects : name){
String n = Object.getName();
String e = Object.getEmail();
model.insertRow(model.getRowCount(),new Object[] { n,e });
}}
try 2:
tableModel.fireTableDataChanged();
try 3:
list_asset.invalidate(); // list_asset is my jTable
list_asset.repaint();
try 4:
((AbstractTableModel) list_asset.getModel()).fireTableCellUpdated(data.size(), 0);
try 5:
jPanel1.revalidate();
jPanel1.repaint();
tableModel.setRowCount(0);
tableModel = new DefaultTableModel(data,columnNames);
list_asset.setModel(tableModel);
I have tried lot many things and while searching on google or Stackoverflow finding these things only which i have already tried.
Please help me in achieving this thing.
Thankx
Upvotes: 0
Views: 159
Reputation: 205875
DefaultTableModel
will notify its listeners of an change, and any listening table will update itself in response. If the approach is your first try didn't work, the problem lies elsewhere. In particular, you should't need to invalidate, revalidate or fire any table model events yourself. In this complete example, the Add Row button invokes addRow()
, which forwards the request to the TableModel
.
private DefaultTableModel dtm = new DefaultTableModel(…) {…}
private void addRow() {
dtm.addRow(new Object[]{
…
});
}
Upvotes: 1