Swing progress bar

0 like 0 dislike
2 views
Gentlemen, I have long tried to understand, how do I get to work prograss bar, but I never did.
The situation such — is a array of objects, it is clear that for-loop.
On the JFrame there is a progress bar.
Code looks like this:
SwingUtilities.invokeLater(new Runnable(){ @Override public void run(){ for(Geoobject geoobject:Geoobjects){ //here you need to change the value ProgressBar //Some actions with the object for(Rajon rajon:geoobject.getRajonsSet()){ //Here you need to change the text on ProgressBar //Some actions with the object } } }); 


the progressBar is not updated while running a for loop.
And as soon as he has completed, the user immediately sees the progress bar is already filled.
And the debug shows that the value he's changing. But on the screen nothing happens.
by | 2 views

3 Answers

0 like 0 dislike
When you say
SwingUtilities.invokeLater(runnable)
this runnable is run from the event dispatching thread-a is the only one. So after a call to
progressBar.setValue(progress)
updated value on the screen, must fly the event. But instead, it is sent to the queue's event dispatching thread-a because your cycle is not yet over.
\r
Do your calculations in another thread, not the event dispatcher. For example, you can do this in the SwingWorker-e
by
0 like 0 dislike
by
0 like 0 dislike
So you are not given control. In the same flow and everything happens when it should get updated? Read here: download.oracle.com/javase/tutorial/uiswing/concurrency/index.html
by

Related questions

0 like 0 dislike
1 answer
0 like 0 dislike
1 answer
0 like 0 dislike
1 answer
asked Apr 12, 2019 by Nikleft
0 like 0 dislike
1 answer
110,608 questions
257,186 answers
0 comments
1,120 users