Click to See Complete Forum and Search --> : Updating table


scheide
April 7th, 2004, 01:34 AM
I've got a JDialog window that contains only a three column JTable, with a checkbox in the first column, looking like this:

CB | Item Nbr | Item Description
---------------------------------------------------

I have code (not part of the dialog window) that loops through each item in the table and as it successfully processes the item, I set the checkbox selected.

The problem I'm having is I want to display the dialog modally and so that it appears on top of the main window, execute the item processing code (which is in the main window) and update the checkbox on the modal window row by row.

All efforts so far have failed. It seems the modal window doesn't want to paint completely (I see just a gray square) and it doesn't refresh and get repainted as each item is processed (the code does get executed, it's just the gui isn't refreshed) -- the dialog and table don't get fully painted until all the components in the table have processed... thus all the checkboxes are selected at first appearance, rather than being checked one at a time.

Currently the code is structured sorta like this and I'm looking for any ideas to make it work properly:

pseduocode, don't worry about the syntax:


setupTable()
{
Runnable r = new Runnable()
{
dialog.show(true); //show JDialog that has table on it
}
SwingUtilities.invokeLater(r);

processComponents();
}


//---------------
processComponents()
{
for (int i =0; i < table.getRowCount(); i++}
{
table.setSelectedRow(i);
if (blah blah blah)
{
dothis dothat etc
table.setValueAt(true, i, 0); //update checkbox
table.invalidate();
table.repaint();
}
}
}

cjard
April 7th, 2004, 02:52 PM
your code will probably generate massive amounts of repaint requests to java, in quick succession.. java deliberately ignores anoy repaint requests while one is pending, and it would appear that issuing further repaint requests cause the original repaint to be delayed even more.. only when the gui is "known to be resting" is the repaint actually done

this is by design, for reasons of performance - java programs spend more time doing their stuff and less time drawing pictures, than, say.. a VB program doing the same thing..

as for how you can solve it.. probably by the same method that people get ProgressBars to work.. have one thread that draws the gui and another that does the work. communicate infrequently, using some kind of event driven system

scheide
April 8th, 2004, 03:47 AM
I ended up downloading SwingWorker from Sun. I put the processComponents method in the "construct" method of the worker, then called the show method on the dialog holding the table.

This worked great...oh the joys of multi-threading (when it works!).