Java GUI JProgressBar not painting

前端 未结 3 1831
独厮守ぢ
独厮守ぢ 2020-12-11 23:50

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 upl

相关标签:
3条回答
  • 2020-12-11 23:59

    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.

    0 讨论(0)
  • 2020-12-12 00:12

    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.

    0 讨论(0)
  • 2020-12-12 00:24

    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.

    0 讨论(0)
提交回复
热议问题