Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
435 views
in Technique[技术] by (71.8m points)

user interface - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...