Java GUI JProgressBar not drawing

I have a problem with the GUI that I would like to understand, but I'm confused about what is happening and I hope that one of you can explain this. The code base is too large to load, but I will explain in detail what is happening:

I have a class ProgessBarthat is JDialogcontaining a swing JProgressBar. I have some getters and setters that change the bar to my liking, however there is a problem.

ProgressBar is generated inside the method myButtonActionPerformed

myButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                myButtonActionPerformed(evt);
            }
        });

when the user clicks on this button, processing begins and appears ProgressBar.

The user currently has JFrame, and this progress bar is displayed in JDialog. After going through the debugging mode in Netbeans, I see that the values JProgressBarchange, but the bar visually stays at 0% while my program is processing, and then instantly switches to 100% the moment it leaves the add action listening method above, almost like as if he was expecting a redraw while out of this listener. What? I do not understand? Is there something I can name that will make it update inside this method myButtonActionPerformed, and not wait until it is useless.

+3
source share
3 answers

, EDT? , AWT/Swing , - , .. , Swing .

. SwingUtilities.invokeLater invokeAndWait, , , , GUI EDT. .

+5

, , , ActionPerformed, . , , .

- - , , .

+1

ActionListener myButtonActionPerformed, , , , , myButtonActionPerformed. , ( ).

Secondly, other people probably nailed it because the likely thing is really hard processing on EDT. You can use SwingWorkers to efficiently route progress / status updates to the GUI thread, as well as move the background process from the EDT.

Third: out of interest are you using NetBeans for your GUI? Looks like its automatically generated code, for me.

+1
source

Source: https://habr.com/ru/post/1752569/


All Articles