• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Refresh JTable

 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How could I refresh the data in JTable on click of a button ?
Thanks
Ravindra
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Amateurs built google. Professionals built the titanic. We can't find the guy that built this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic