<Swing Dev> May Component.isFocusOwner() be called by non-edt threads?
artem.ananiev at oracle.com
Tue Nov 1 06:30:14 PDT 2011
On 11/1/2011 4:56 PM, Christopher Deckers wrote:
>> Of course all Swing methods (if javadoc doesn't say the opposite) must be
>> called from EDT thread.
> Which makes me remember that I recently received a bug report from
> someone using the Java Access Bridge (JAB).
> The bug is that JAB calls "Component.isFocusOwner()" outside of the
> EDT but my component enforces correct threading calls. Because the
> Javadoc does not say that it can be called from outside the EDT, I
> throw an exception on that call (or rather, in
> I don't know much about JAB, but the user seemed to imply he did not
> create the JAB thread so I assume JAB did it.
> My question: is it allow to call "Component.isFocusOwner()" or
> "Component.hasFocus()" outside the EDT? If it is the case, shouldn't
> the Javadoc say so?
This is a gray area. Many Swing methods are actually inherited from AWT,
which is a thread-safe library. However, some methods are overridden in
JComponent and other classes, some methods can query other Swing
objects, many actions can fire new events, etc. So would strongly
recommend to access all the UI objects on EDT, event to call methods
JavaDoc for Component.isFocusOwner() doesn't contain any threading
notes, which is correct, as AWT is thread-safe. We can probably include
the warnings to JComponent.isFocusOwner(), but... in this case we should
override all the AWT methods and correct JavaDoc comments. Instead, we
just say "Swing should be operated on EDT only" in a single place, and
assume this statement is true for all the methods.
More information about the swing-dev