Search code examples
javaswingjtablejscrollpaneabstracttablemodel

How to refresh a JTable on JScrollPane


as many other persons, I want my JTable being actualized after adding rows. Here you can see the fragments of my code.

ArrayList<> foundR = new ArrayList<FoundResult>();

JTable resultsTable = new JTable();
AbstractTableModel resultsModel = new FoundResultTableModel(foundR);//<- model which contains FoundResults as rows
JScrollPane scroller = new JScrollPane();

resultsTable.setModel(resultsModel);
scroller.add(resultsTable);
this.add(scroller, BorderLayout.CENTER);// a big panel which contains scroller and other components

Then a add ActionListener that uses following method:

foundR.clear();
foundR.addAll( ...);//<- so the foundR ArrayList is refreshed
resultsModel.fireTableDataChanged();
resultsModel.fireTableStructureChanged();

rowCount of results Model shows that the model is refreshed (the number of rows differes from the previeous variant), but the table still doesn't appear. I tried to insert resultsTable.repaint() but it also dind't help.

UDP(NB) I discovered, that the problem is concentrated in this scroller Panel.

If I add the table directly to the big panel it is refreshed (but I cannot see all the results, since I cannot scroll down)

 this.add(resultsTable, BorderLayout.CENTER); 

If I use a scroller, nothing is shown. Do you know why?


Solution

  • Add this to your code,

    resultsTable.revalidate();
    

    Since your model has been changed but the change in model should be notified to the table also.