简体   繁体   中英

Java GUI JProgressBar not painting

I have a GUI problem that I would like to get sorted out, but I am baffled as to what's happening and hope one of you can explain it. The code base is way too large to upload however I will explain in detail what's happening:

I have a class ProgessBar which is a JDialog containing a swing JProgressBar . I have some getters and setters to change the bar to my liking however here comes the issue.

The ProgressBar is spawned inside of a method myButtonActionPerformed

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

essentially when the user hits this button, the processing begins and the ProgressBar is spawned.

The user currently has a JFrame in front of them and this progress bar is popped up in a JDialog . After stepping through debug mode in Netbeans, I can see the JProgressBar 's values getting changed, but the bar visually stays at 0% while my program is processing then instantly jumps to 100% the moment it leaves the add action listener method above, almost as if it was waiting to repaint until out of that listener. What am I not understanding? Is there something I can call that will force it to update inside of this myButtonActionPerformed method instead of waiting until it's useless.

Are you doing whatever takes that time in the EDT? Keep in mind that AWT/Swing have a dedicated thread that does GUI work – handling event handlers, repainting the GUI, etc. If you do long-running things on that thread, Swing will not repaint.

Try performing your task in another thread and update the progressbar from there accordingly. Use SwingUtilities.invokeLater or invokeAndWait to update the progressbar, though, to ensure that the GUI updates happen on the EDT. Otherwise things get very weird.

The reason it doesn't update is because your UI thread is busy in the ActionPerformed listener doing processing. If you try and interact with your application, you'll notice that nothing on the UI is responsive.

The solution to this is to not do processing on the UI thread - launch another thread, do the processing on that, and call back to the UI thread to update the progress bar.

First your anonymous ActionListener simply forwards to a method called myButtonActionPerformed, so obviously the issue is not in the posted code, but rather in something that happens or is caused to happen inside myButtonActionPerformed. So that would be the relevant code to post (if possible).

Second other people probably nailed this already because the likely thing is indeed heavy processing on the EDT. One can use SwingWorkers to efficiently route the progress/status updates to the GUI thread as appropriate and also move the background processing off the EDT.

Third: out of interest, do you happen to use NetBeans for your GUI design? Looks like its automatically generated code, to me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM