Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Refresh JTable

 
ravindra janapreddy
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How could I refresh the data in JTable on click of a button ?
Thanks
Ravindra
 
Alex Gregory
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you could call the repaint method of the table, this works if you beforehand you call the setModel method on the table, and afterwards calling repaint will just repaint the table using the model you binded to the table before.
 
ravindra janapreddy
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The repaint doesn't seem to work for me.
Please look at my partial code:
TableModel model = new EditableTableModel(columnTitles, dataEntries);
table = new JTable(model);
public void actionPerformed(ActionEvent ae) {
DataInfo[] info = client.getInitialFlightInformation(count);
for ( int i = 0; i < info.length; i++ ) {
if ( info[i] != null ) {
dataEntries[i] = info[i].getValues();
for ( int j = 0; j < len; j++ ) {
table.setValueAt( dataEntries[i][j] , i, j );
} // end of for
} // end of if
} // end of for clause
table.repaint();
} // end of actionPerformed
Thanks
Ravindra
 
ravindra janapreddy
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have made my class to implement TableModelListener
model = new EditableTableModel(columnTitles, dataEntries);
table = new JTable(model);
model.addTableModelListener(this);

and implemented the following method:
public void tableChanged(TableModelEvent e) {
System.out.println("tableChanged");
table.tableChanged(e);
}
Still the table doesn't refresh.
Ravindra
 
ravindra janapreddy
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have also implemented by the following methods in EditableTableModel:
public void setValueAt(Object value, int row, int column) {
dataEntries[row][column] = value;
fireContentsChanged();
fireTableDataChanged();
}
public void addTableModelListener(TableModelListener l) {
if ( !tableModelListeners.contains(l) ) {
tableModelListeners.add(l);
}
}
public void removeTableModelListener(TableModelListener l) {
if ( tableModelListeners.contains(l) ) {
tableModelListeners.remove(l);
}
}
public void fireContentsChanged() {
TableModelListener ldl; //temp var
TableModelEvent e = new TableModelEvent(this);
for(int i=0; i<tableModelListeners.size(); i++){
ldl = (TableModelListener) tableModelListeners.get(i);
ldl.tableChanged(e);
}
}//end method
-- Ravindra
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Here is the only two lines of code that I needed to refresh the JTable with new data based on their search.
I changed my actual parameter for the new FlightTableModel because I don't want to give it away. But you should know how you would set that variable based on how you load the JTable the first time.
Mark
 
Amit Kr Kumar
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark
Another way to refesh the table is just call fireTableDataChanged() method inside setData(..)
method of TableModel.
Hope this helps
Amit
 
Ramesh kumaar
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mark,
What u said will work for only to construct a JTable with all the initial values but i guess their requirement is to update a row dynamically. In this case the below code may not be usefull. They have to use setValueAt() to achive this. Here some used EditableTableModel i dont know what is the need for that.

Mark
Spritzler
code:


FlightTableModel tableModel = new FlightTableModel(parameterHere);
dataTable.setModel(tableModel);
-rameshkumar
 
ravindra janapreddy
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My EditableTableModel is same as FlightTableModel. Anyway , I have renamed my class as FlightTableModel and refactored the usage in the rest of the project.
class SearchFlightAction extends AbstractAction {
public SearchFlightAction(String label) {
super(label);
}
public void actionPerformed(ActionEvent ae) {
String origin = (String)combo1.getSelectedItem();
String destination = (String)combo2.getSelectedItem();
tring carrier = (String)combo3.getSelectedItem();
String criteria = SearchCriteriaBuilder.parse(origin, destination, carrier);
DataInfo[] info = client.searchFlight(criteria.toString());
for ( int i = 0; i < info.length; i++ ) {
if ( info[i] != null ) {
dataEntries[i] = info[i].getValues();
if ( dataEntries[i][1].equals(origin) && dataEntries[i][2].equals(destination) && dataEntries[i][3].equals(carrier) ) {
for ( int j = 0; j < len; j++ ) {
table.setValueAt( dataEntries[i][j] , i, j );
} // end of for
}
} // end of if
} // end of for clause
model = new FlightTableModel(columnTitles, dataEntries);table.setModel(model);
}
} // end of class
The data in the table is not yet refreshed
Ravindra
 
ravindra janapreddy
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am also calling fireTableDataChanged inside the setValueAt but the data is not refreshed in the table.
public void setValueAt(Object value, int row, int column) {
dataEntries[row][column] = value;
fireContentsChanged();
fireTableDataChanged();
}
Ravindra
 
Ramesh kumaar
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mark,
What u said will work for only to construct a JTable with all the initial values but i guess their requirement is to update a row dynamically. In this case the below code may not be usefull. They have to use setValueAt() to achive this. Here some used EditableTableModel i dont know what is the need for that.

Mark
Spritzler
code:


FlightTableModel tableModel = new FlightTableModel(parameterHere);
dataTable.setModel(tableModel);
-rameshkumar
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic