Handling the Event Dispatch Thread

前端 未结 4 1371
一个人的身影
一个人的身影 2021-01-01 06:22

I have a question about the \'Event Dispatch Thread\'. I have a Main class that is also a JFrame. It initialises the rest of the components in the code, some of them do not

4条回答
  •  自闭症患者
    2021-01-01 07:16

    That is generally sufficient until you start making use of background threads for calculations, data acquisition, etc. Then you need to start being careful to verify that you are on the EDT prior to altering a swing component or its model.

    You can test whether you're executing on the EDT with:

        if (SwingUtilities.isEventDispatchThread()) {
            // Yes, manipulate swing components
        } else {
            // No, use invokeLater() to schedule work on the EDT
        }
    

    Also, see the SwingWorker class for details on how to hand off work to a background thread and process results on the EDT

提交回复
热议问题