Swing - progress bar

Asked by amy crehore

Gentlemen, I have been trying for a long time to figure out how to get the program bar to work, but I didn’t have anything.
The situation is this - a large array of objects is processed, it is clear that in the for-loop.
There is a progress bar on the JFrame.
The code looks like this:
 

SwingUtilities.invokeLater(new Runnable(){

    @Override
    public void run(){
    for(Geoobject geoobject:Geoobjects){
        //здесь нужно поменять значение прогрессбара
        //Какие-то действия с объектом
        for(Rajon rajon:geoobject.getRajonsSet()){
            //Здесь нужно поменять текст на прогрессбаре
            //Какие-то действия с объектом
        }
    }
});


progressBar is not updated while the for-loop is running.
And as soon as he runs, the user immediately sees the progress bar already filled.
Moreover, during debugging, it is clear that its value is changing. But nothing happens on the screen.

Comments:
And what exactly is happening? Is it not updated? - juliana
Let's just say that it is not updated while the for-cycle is working.
And as soon as he runs, the user immediately sees the progress bar already filled.
Moreover, during debugging, it is clear that its value is changing. But nothing happens on the screen. - lisa s

Answers

alex popa
When you say
SwingUtilities.invokeLater(runnable)
, this runnable starts from event dispatching thread -a, which is the only one. In order for the value on the screen to be updated after the call to
progressBar.setValue(progress)
, the event must fly. Instead, it is sent to the event dispatching thread queue, because your cycle is not over yet.

Perform your calculations in a different thread, not in the event dispatcher-e. For example, you can do it in SwingWorker-es
Replies:
And I, fool, thought that SwingUtilities creates a new thread, and only in it ...
Thanks, I will study. - olivia todman
Good luck :) - kizhepat
solenn
ruth anne
So you do not give control. In one stream, everything happens when it should be updated? Read here: download.oracle.com/javase/tutorial/uiswing/concurrency/index.html
Replies:
That’s my problem - I don’t understand.
I have an instance of JProgressBar in EventDispatcher.
from another thread (which is called via SwingUtilities in the question) A change is made to the property of the progress bar, which is the main thread. And does not work.
I understand that I am doing something wrong, but I just cannot understand it in any way - how is that right? - prahallad badami
Are there any good and complete manuals on HTML5 and CSS3 in Russian? :: Who can advise the manual for setting up a server for mass mailings (not spam)? :: How to make HTML table from 3D bricks? :: What software for security testing is optimal in terms of price / performance ratio? :: Is there an RSS for the q & a service in habra?
Leave Repply for Swing - progress bar
Useful Links