How do I use SwingWorker in Java?

后端 未结 2 1875
借酒劲吻你
借酒劲吻你 2020-11-22 06:32

Related to my previous question: Call repaint from another class in Java?

I\'m new to Java and I\'ve had a look at some tutorials on Swi

2条回答
  •  孤城傲影
    2020-11-22 07:09

    Agree:

    Running long-running tasks on the Event Dispatch Thread (EDT) can cause the GUI to lock up.

    Do not agree:

    so one of the things which were done is to use SwingUtilities.invokeLater and invokeAndWait which keeps the GUI responsive..

    invokeLater still runs the code on the EDT, and can freeze your UI!! Try this:

    SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        Thread.sleep(100000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            });
    

    At least I, cannot move my mouse once I click the button which triggers the actionPerformed with the above code. Am I missing something?

提交回复
热议问题