How to update JFrame Label within a Thread? - Java

后端 未结 5 1488
南笙
南笙 2021-01-19 02:20

I have tried a lot, but can\'t seem to get it to work.

I was told to use EDT with the following example.

    SwingUtilities.invokeLater(new Runnable(         


        
5条回答
  •  暖寄归人
    2021-01-19 02:47

    So there is an EDT (Event Dispatch Thread). All actions that appear on your screen are executed by the EDT. There is only one EDT per Swing application.

    You are in some arbitrary thread and you want to update the GUI through that thread? Well like I said there is only one EDT for each swing application, so you have to tell that EDT to display the label (or whatever context you want).

    The idea here, is you push this Runnable onto a queue that the EDT pulls from. Eventually, your runnable will be processed by the EDT when all other actions before it are completed.

提交回复
热议问题