aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Table will not refresh? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Table will not refresh?" Watch "Table will not refresh?" New topic
Author

Table will not refresh?

Shannon Sims
Ranch Hand

Joined: Jul 03, 2003
Posts: 197
When I invoke this method from the server-side code, the table doesn't reload with the latest data. After doing some research, I came across a method "fireTableDataChanged()" on the DefaultTableModel. I'ved tried casting this table as a DefaultTableModel and invoking this method, however still no luck?

private void buildContractorResultTable()
{
JTable table = new JTable( aModel.getMatchingCriteria(),
aModel.getColumnNames() );
table.setPreferredScrollableViewportSize(
new Dimension( 500, 100 ) );
TableColumn widestColumn = table.getColumnModel().getColumn(2);
widestColumn.setPreferredWidth( 150 );

JScrollPane scrollpane = new JScrollPane( table );
JPanel tablePanel = new JPanel();
tablePanel.setLayout( new FlowLayout( FlowLayout.CENTER ) );
tablePanel.add( scrollpane );

this.getContentPane().add( tablePanel );
}

All help is appreciated.
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
I'm a little confused. This code does not update a table, but rather creates a new table. Whatever that 'aModel' object you have there is, it's not being used as the TableModel.

I'm assuming that you are using the constructor for the JTable that takes an Object[][] for data and an Object[] for the column names. In this case the JTable will create it's own DefaultTableModel. You can get to that model by calling the JTables getModel() method.
Shannon Sims
Ranch Hand

Joined: Jul 03, 2003
Posts: 197
Hi Steven,
Thanks for your response. Yes, I'm constructing the table with two Vector objects. Every time I call this method, it would then reconstruct the table with the new values...or at least that's what I thought would occur. I also tried the following code and still no luck:

JTable table = new JTable( aModel.getMatchingCriteria(),
aModel.getColumnNames() );
table.setPreferredScrollableViewportSize( new Dimension( 500, 100 ) );
TableColumn widestColumn = table.getColumnModel().getColumn(2);
widestColumn.setPreferredWidth( 150 );

DefaultTableModel model = (DefaultTableModel) table.getModel();
model.fireTableDataChanged();

JScrollPane scrollpane = new JScrollPane( table );
JPanel tablePanel = new JPanel();
tablePanel.setLayout( new FlowLayout( FlowLayout.CENTER ) );
tablePanel.add( scrollpane );

this.getContentPane().add( tablePanel );

Thanks for your help. I don't have any books that address this issue.
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
My guess would be that your problem is not with the JTable refreshing itself. You shouldn't need to create a new JTable each time unless you are building a new JFrame each time around.

In this case calling the fireTableDataChanged() method will have no effect as the data hasn't actually changed, you have created a new JTable with only the new data. If you are seeing old data then you are either building this new JTable with old data or you are seeing and old JTable that is still around.

How drastic is the change in data? Is it just a new row added or a row removed? Completely different data requiring new/different columns?

If you just need to add and remove rows, the DefaultTableModel has methods to do that, if you need to have a few ready to go table layouts you might want to subclass one of the TableModels.

Give me a little more information about what your doing. I have some code at home that uses a JTable for a few different lookups, should jog my memory on how to make it work.

Also when you post code put it in code tags (you will see a code button under where you write a new post). You can also go back and edit your posts by clicking on the paper and pencil icon.
Shannon Sims
Ranch Hand

Joined: Jul 03, 2003
Posts: 197
Thanks Steven for the quick response! I'm working on my Java Developer certification (B&S). This table is used to display the results based on the user's search criteria. Initially, the table is loaded without any rows. Once the user clicks on the "search" button, I then return the results based on the search criteria.

The change to the table could be as little as returning zero to eight rows of data. To minimize the complexity of the UI, my approach would not allow the user to update the rows, but to modify another UI based on the selected row. If the user updated the UI, it would then in turn update the selected row.

Ok, that explains why the table wouldn't update...makes total sense. So I guess now I need to try a different approach.

Sure thing, I will put the code in tags next time I post code.

Thanks again Steven.
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
Here is what I would suggest to fix your problem.

1) Make the JTable a class level variable so you only create it once
2) Place the JTable in your frame in some method other than the one used to populate the table
3) When the data changes do the following:

I didn't actually compile the code so there may be a typo or two. The DefaultTableModel will use all the correct fireXXX methods internally when you call the setDataVector method.
Shannon Sims
Ranch Hand

Joined: Jul 03, 2003
Posts: 197
Ok, I'll give it a try tonight when I get home from work. Thanks for all your help! It is very much appreciated.
Shannon Sims
Ranch Hand

Joined: Jul 03, 2003
Posts: 197
Hey Steven, it worked, it worked!!! How exciting! THANK YOU SO MUCH for you help!
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
glad to help
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Table will not refresh?