When to use SwingUtilies.invokeAndWait/invokeLater

后端 未结 4 1914
半阙折子戏
半阙折子戏 2021-02-06 01:42

I read somewhere that for any thread that affects the visuals of the gui it should be ran in the EDT using SwingUtilities.invokeAndWait/invokeLater

For a basic gui, is i

4条回答
  •  不要未来只要你来
    2021-02-06 02:26

    Yes, if you touch a Swing object you have to do it on the EDT. In most cases you are already there, but if not, use the SwingUtilities classes. The reason for this is that the Swing classes are not multi-threaded, so you are likely to cause nasty problems if you access it on other threads. And it could be that setVisible() is doing a lot of things under the covers to make something display (like re-laying things out). Better to be safe.

提交回复
热议问题