Problem with repainting
Hi all
I have a serious problem that I cant resolve, I will try as best to be as clear as I can be.
I have a JProgressBar, the value gets set else where and passes this value into the progress bar class, the problem I am having is two fold firstly, even though the value is been constantly set , a visible change is only seen at the end of the whole process, the second part of the problem is this, if the screen is minimized and maximized the whole screen remains orange until the process is complete, then the screen returns. The strange thing is that I am not calling a validate or repaint method after the process is done.
the second problem is seen throughout the whole program for instance I am streaming results into a JTable it used to update line by line, now the whole table only displays once the last value has been streamed in, if the screen is minimized and maximized the orange screen returns.
so to summarise anywhere in the program where a component needs to updated it isn't (even if I repaint and validate) and once a process is complete everything displays and that Damm orange.
Any thoughts out there would be appreciated
thanks in advance Brian
You are performing time consuming tasks on the event dispatch thread (EDT).
For example, something like this...
public void actionPerformed(ActionEvent e)
{
doSomethingThatTakesQuiteAWhile();
}
...will lock up the GUI, since nothing (ie painting, events etc) can be processed until your method returns.
Google for "swing threads" and hit "I'm feeling lucky."
> Well I would agree with you in theory the problem is
> that for example the result set will only return 30
> rows and then the value is set, then another 30 rows
> are returned and the progress bar set, even if i
> disable the work that is done on the result sets and
> just set the values of the Progress bar the problem
> persists.
That's not the point. The point is that you can set the progress position all you like and nothing will happen until you release the dispatcher thread to allow it to refresh what you are seeing.
When you make a change like this all the repainting is added to the dispactcher's TODO list. But if the dispatcher is still running an [b]actionPerformed[/b] method then it's not looking at that list.
You need to create a seperate Thread, usually called a "worker" thread to do you database work. Don't do anything in an actionPerformed method which takes more than a fraction of a second.
Typically the actionPerformed method will just disable the control, then create and start a thread to do the work, then exit (the thread re-enables the control when it's done).
Something like;
[code]
private Action launchSQL = new AbstractAction("Go") {
public void actionPerformed(ActionEvent e) {
setEnabled(false);
Thread worker = new Thread(new Runnable() {
public void run() {
try {
doSQLStuff();
}
finally {
setEnabled(true);
}
}}, "SQL Worker thread");
worker.start();
}
};
[code]