How do you use the Event Dispatch Thread?

后端 未结 4 1262
夕颜
夕颜 2020-12-09 20:50

I learned about how swing isn\'t thread-safe. Delving deeper, I discovered that every modification to a swing component must be done on the Event Dispatch Thread in order to

4条回答
  •  伪装坚强ぢ
    2020-12-09 21:12

    The trick is that when swing calls you it will ALWAYS be in the EDT, so you don't have to worry about it.

    However if you are in a timer or an action triggered by some other external event, your main thread or any other thread you've created then yes, you have to use invokeLater or invokeAndWait.

    In other words, yes swing does do "it" automatically. Needing to use invokeXx is so rare that if swing were to do it internally it would waste too much time.

    Many java programmers never figure this out and it can cause some pretty nasty hard-to-find problems with drawing your GUI. I do wish swing threw an exception when you called it not using the EDT--Java would have a better reputation when it came to professional GUIs if it did because there would be less crap out there.

提交回复
热议问题