Swing GUI is not updating

后端 未结 3 1055
自闭症患者
自闭症患者 2021-01-28 08:06

I have simple Java Swing application which uses zip4j to encrypt and unpack zip file. It\'s done by this part of code:

ZipFile zipFile = new ZipFile(\"dataStorag         


        
相关标签:
3条回答
  • 2021-01-28 08:33

    You shouldn#t do your zipping on the event dispacher thread (which is where all your event-handling takes place). Create a SwingWorker or someething like it to offload your heavy duty on a separate processing thread that can then inform the progress bar that can be updated on the EDT. With your solution all updates to the progress bar can only be processed when the EDT is free again, that is after your zip-operation is finished.

    0 讨论(0)
  • 2021-01-28 08:43

    Please read Concurrency in Swing.

    What you are doing is using up all resources of the EDT by sleeping and updating, not leaving any time for it to actually redraw your GUI. The EDT is meant for small operations on the GUI. You should never call Thread.sleep() on the EDT.

    What you could do is make a Timer that would run your check every second until the check passes. That way the EDT would be free to not freeze.

    A much better way of doing this is by using a SwingWorker. It has methods that do your specific thing:

    • A task to do in the background (In your case - unzip)
    • A method to publish a partial result to the GUI (in your case % done)
    • A method to react to partial results (In your case - update progress)
    • A method to invoke when done (Not shown in your case, but useful anyway).
    0 讨论(0)
  • 2021-01-28 08:48

    Wrap the call pbEncryptionProgress.setValue(progressMonitor.getPercentDone()); in SwingUtilities.invokeAndWait

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