aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Refresh JTable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Refresh JTable" Watch "Refresh JTable" New topic
Author

Refresh JTable

ravindra janapreddy
Ranch Hand

Joined: Jan 30, 2001
Posts: 84
How could I refresh the data in JTable on click of a button ?
Thanks
Ravindra


SCEA, SCBCD, SCWCD, SCJD, SCJP
Alex Gregory
Ranch Hand

Joined: Jun 15, 2002
Posts: 46
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

Joined: Jan 30, 2001
Posts: 84
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

Joined: Jan 30, 2001
Posts: 84
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

Joined: Jan 30, 2001
Posts: 84
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

Joined: Feb 05, 2001
Posts: 17260
    
    6


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


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Amit Kr Kumar
Ranch Hand

Joined: Feb 08, 2002
Posts: 100
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

Joined: Mar 19, 2002
Posts: 146
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

Joined: Jan 30, 2001
Posts: 84
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

Joined: Jan 30, 2001
Posts: 84
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

Joined: Mar 19, 2002
Posts: 146
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Refresh JTable