How do I know if I'm on the event dispatch thread?

后端 未结 2 1428
猫巷女王i
猫巷女王i 2021-01-04 02:36

1.Consider my code is on some line of a JPanel that I have, am I automatically on EDT?

2.Same question for all other classes which are not belong to GUI, JPanels o

2条回答
  •  梦毁少年i
    2021-01-04 03:07

    As per my comment: When all else fails, the SwingUtilities class has a static method that you can use: SwingUtilities.isEventDispatchThread()

    With regards to number 3) definitely use a background thread.

    And as far as I know, there is no "general rule". Yes code in your GUI should be on the EDT, but if you have a bug somewhere, it may not be, though usually it is. Same for Swing listeners.

提交回复
热议问题