1) Is it sometimes legal to make all these calls because of the Swing thread?
There are a few exceptions (setting the text value of a text field, for example, makes EDT automatically proxy for you), but there are no situations where it is better to do this. If you perform many updates, you can do them all with one click of EDT (one invokeLater () call) instead of separate calls, but even such sorting very rarely helps. Long and short: perform operations on Swing components from the EDT. This includes reading and writing.
2) What is the effect on the Swing thread and why is the user interface less responsive when I call it from the outside?
Well, EDT is responsible for updating the GUI. If you call from outside, this is no less “responsive” —that is, there are no actual low-level system calls that do not update the user interface. What probably happens in your application is that the original developers were lucky and the state in the swing component changed, without creating a really unpleasant race condition. Then, some other event causes a redraw on the EDT, which leads to the update of the component. This may seem like a “lack of responsiveness,” but what actually happens is the “lack of a screen update.”
EDT is just a regular thread, but it is slightly different in that it runs in a narrow loop that processes GUI-related signals (like drawing commands). The semantics of publishing these types of commands in EDT are really different from what we usually see as Java threads (this involves sending operations to the message pump).
Long and short - all of these Javadocs that say “only interact with Swing objects on EDT” are written for some reason. Do not get involved in this. If you want to do background processing, great, but you are responsible for proxying the interaction with J * components back to EDT (most often with invokeLater ()).
Kevin day
source share