How to update JFrame Label within a Thread? - Java

后端 未结 5 1487
南笙
南笙 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:51

    I recommend you get the book Filthy Rich Clients. There's a chapter where they explain Swing's threading model to great detail.

    Basically in Swing, any code that modifies the GUI should be executed in the Event Dispatcher Thread. The SwingUtilities class that you are using there provides you with an easy way to post events to the event queue that is then dispatched by the EDT. That's what the invokeLater method does, it takes a new Runnable() as argument which is ultimately executed on the EDT.

    From the book:

    The invokeLater() implementation takes care of creating and queuing a special event that contains the Runnable. This event is processed on the EDT in the order it was received, just like any other event. When its time comes, it is dispatched by running the Runnable’s run() method.

提交回复
热议问题