Java Swing - running on EDT

前端 未结 4 1506
一整个雨季
一整个雨季 2020-12-06 17:25

I have a couple of questions with regards to Swing and using EDT for GUI updates. I just started reading on this stuff so I am a full beginner in this area:

  1. Wh
4条回答
  •  一个人的身影
    2020-12-06 17:58

    1. In Java GUI applications, main() method is not long lived, after scheduling the construction of GUI in the Event Dispatcher Thread, the main() method quits...and Now its EDT's responsibility to handle the GUI.

    2. So we don't need to start our app on the EDT, its automatically done.

    3 Always keep the UI work on the UI thread, and Non-UI work on the Non-UI thread.

    So Always keep your EDT thread, which is the GUI thread only for GUI work.

    Eg:

    public static void main(String[] args){
        EventQueue.invokeLater(new Runnable(){
              public void run(){    
                myframe.setVisible(true);
             }
         }
    }
    

    4. Create a separate Non-UI thread to handle that long time taking method.

    5. You can simply use a Thread or use SwingWorker which is specially introduced into Java to sync the UI and Non-UI thread.

    6. SwingWorker doesnt ensure that done() method is run on EDT, but sync the output of it to EDT thread, which is the GUI thread.

提交回复
热议问题