JTable Reloading/Refreshing/Updating/Whatever.

Reply

Join Date: Jun 2005
Posts: 7
Reputation: deineMutti is an unknown quantity at this point 
Solved Threads: 0
deineMutti deineMutti is offline Offline
Newbie Poster

JTable Reloading/Refreshing/Updating/Whatever.

 
0
  #1
Jun 23rd, 2005
Hello Java powers that be,

I've implemented a JTable that I can add/delete rows of information to/from, but I can't actually see the changes until I do something stupid like resize the columns. I currently use a "fireTableCellUpdated(row, col);" doo-hicky in my AbstractTableModel(whenever data is changed) that I thought would do the trick, but I guess not.

I'm not so skilled with the Java/GUI programming stuff, so let me know what other information you might need to know to solve this problem.

Unrelated Problem: Does anyone know how to change the color of the numbers on a JSlider? (I've tried setBackground() and setForeground())

peace.
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 2,108
Reputation: server_crash is on a distinguished road 
Solved Threads: 18
server_crash server_crash is offline Offline
Postaholic

Re: JTable Reloading/Refreshing/Updating/Whatever.

 
0
  #2
Jun 23rd, 2005
Right after you add a row or delete it, revalidate the table.

table.revalidate();

call that on the table, NOT the table model.
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 2,108
Reputation: server_crash is on a distinguished road 
Solved Threads: 18
server_crash server_crash is offline Offline
Postaholic

Re: JTable Reloading/Refreshing/Updating/Whatever.

 
0
  #3
Jun 23rd, 2005
Also, is it really necessary that you use the abstract table model? I'm using default right now and it's much easier to use. It simply has a method called addRow() and addColumn() that would probably suit your needs and make life easier on you.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 7
Reputation: deineMutti is an unknown quantity at this point 
Solved Threads: 0
deineMutti deineMutti is offline Offline
Newbie Poster

Re: JTable Reloading/Refreshing/Updating/Whatever.

 
0
  #4
Jun 23rd, 2005
YES! Was about to write "Thanks for the reply server_crash, but it didn't seem to work....." when I realized I was calling revalidate(); on the instance of my table-containing panel class, not the actual table.

For some reason it doesn't update on the first addition, but I think that's some weirdness on my end...something that I can fiddle around with.

As far as the question of whether or not I'd be better off just using a default model....well, I don't know. I might be, but I really wouldn't know because I'm just kind of piecing this application together from lots and lots of random Java GUI lessons learned.

Thanks again for the help.

peace.
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 2,108
Reputation: server_crash is on a distinguished road 
Solved Threads: 18
server_crash server_crash is offline Offline
Postaholic

Re: JTable Reloading/Refreshing/Updating/Whatever.

 
0
  #5
Jun 23rd, 2005
I did this just the other day! I would suggest using the DefaultTableModel, you'll have an easier time with it.

This was my code:

  1. int c = dtm.getRowCount();
  2. for (int i=c-1; i>=0; i--)
  3. {
  4. dtm.removeRow(i);
  5. mortgageTable.revalidate();
  6. }

dtm is a DefaultTableModel
mortgageTable is the JTable

Compare your code with mine. It will be somewhat different, but overall it should look similar. Again, I really think you should use the DefaultTableModel instead.
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 3
Reputation: woo37830 is an unknown quantity at this point 
Solved Threads: 0
woo37830 woo37830 is offline Offline
Newbie Poster

Re: JTable Reloading/Refreshing/Updating/Whatever.

 
0
  #6
Jun 12th, 2008
I have a very simple frame that retrieves the list of pdf files in a directory.
Using an extension of a DefaultTableModel, it then loads each filename preceded by a Boolean and followed by a blank string. When I click on an action button "Scan", it loops through the rows from the table model and if it is "checked", it scans the file. Each time it changes pages in the scan, it calls a method "change" in the frame holding the table with the filename as an argument. The method looks up the fileName in the model and does a tablemodel.setValueAt(str, row, 2), where row is the row located, 2 is the location of the column that had an empty string, and str contains "2 or 5" for instance, then "3 of 5", etc. Since I am using setValueAt on a DefaultTableModel and haven't overridden any methods, I expect to see the value in the 3rd column ticking away 2 of 5, 3 of 5, 4 of 5, etc.

After all files have been processed, the last of the values shows up for all rows simultaneously like "3 of 3" and "5 of 5" showing me that I have been setting the values. What's missing is the changing of that cell on the screen in real-time.

I've tried fireTableDataChanged(), revalidate on the table, etc. NOTHING.
<code>
// section where row is found and model is updated. The msg contains
// "3 of 5", "4 of 5", etc.
int row = ((MyTableModel)aTable.getModel()).findRow(fileName);
// update the Type info
// System.out.println("Looking for <"+fileName+"> found row "+row);
if (row == -1)
return;
System.out.println("Updating row " + row + " with " + msg);
((DefaultTableModel)aTable.getModel()).setValueAt(msg, row, 2);
// various attempts to cause table to show the message.
//aTable.revalidate();
//((AbstractTableModel)aTable.getModel()).fireTableDataChanged();
//aTable.updateUI();

</code>
The class that extends DefaultTableModel
<code>
class MyTableModel extends DefaultTableModel
{
private int rows = 0;
public MyTableModel(Vector dataVector, Vector columnNames)
{
super(dataVector, columnNames);
rows = dataVector.size();
}

public Class getColumnClass(int c)
{
if (c == 0)
return Boolean.class;
else
return super.getColumnClass(c);
}

/*
* Don't need to implement this method unless your table's editable.
*/
public boolean isCellEditable(int row, int col)
{
if (col == 0 || col == 2)
return true;
return false;
}

public int findRow(String fileName)
{
for (int i = 0; i < rows; i++)
{
if (fileName.equals((String)getValueAt(i, 1)))
return i;
}
return -1;
}
}
</code>
I'm showing the loop also just so it can be seen.
<code>
OpenHandler handler = new OpenHandler(getQueriable());
int rows = ((MyTableModel)aTable.getModel()).getRowCount();
long start = System.currentTimeMillis();
for (int i = 0; i < rows; i++)
{
boolean val = ((Boolean)aTable.getModel().getValueAt(i, 0)).booleanValue();
if (!val)
continue;
String fileName = (String)aTable.getModel().getValueAt(i, 1);
String path = aDir.getAbsolutePath() + File.separatorChar + fileName;
handler.scanPages((Editable)getQueriable().getViewer(), path);
}
long stop = System.currentTimeMillis();
System.out.println("\nTime to scan "+rows+" files is "+(stop - start)/1000+" seconds");
</code>
The handler scans the pages and as it does it calls the first piece of code and very clearly prints the message to the console showing the progress, but NOT updating the gui until it's completely done, then it does.
I can find no reason why the JTable should not be showing me the "progress" as I loop through the rows.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,438
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 510
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: JTable Reloading/Refreshing/Updating/Whatever.

 
0
  #7
Jun 12th, 2008
Swing is a single-threaded event model. If you are executing the code that updates the table in a button action listener then you are essentially "holding-up" processing other UI events like repaints, table updates, etc.

If you want the table to update over the course of a long running process you will need to perform that processing in a separate thread and push UI updates onto the event dispatch thread.

This tutorial may help: http://java.sun.com/docs/books/tutor...ncy/index.html
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 3
Reputation: woo37830 is an unknown quantity at this point 
Solved Threads: 0
woo37830 woo37830 is offline Offline
Newbie Poster

Re: JTable Reloading/Refreshing/Updating/Whatever.

 
0
  #8
Jun 13th, 2008
Great advice! Thanks! I finally understand what's been bothering me in so many cases! Took me about 10 minutes to fix my code so it does precisely what I wanted.

This link on single threaded swing should be advice to all with questions about non-responsiveness of GUI's to update events!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC