Javafx embedded in swing multithreaded toolkit issue

Artem Ananiev artem.ananiev at oracle.com
Wed Mar 21 02:23:42 PDT 2012


On 3/21/2012 6:17 AM, Pedro Duque Vieira wrote:
> Hi,
>
> I've been working for a while on a swing app that has a javafx scene
> embedded on it.
>
> So in this scenario we have two UI threads, one for the swing part and one
> for the javafx scene (the application thread).
> This situation creates a multithreaded ui toolkit which is basically a
> nightmare for development.
>
> I don't know if this is possible but it I think it would be best to merge
> the swing and the javafx application thread into one when on this scenario.

In JavaFX/SWT bridge (FXCanvas) we do exactly this: FX application 
thread is the same as SWT event thread. Unfortunately, in JavaFX/Swing 
case it is not possible.

In a few words, Swing event dispatch thread only pumps Java events. All 
the native events are dispatched on another thread, which is called AWT 
toolkit thread. This thread cannot be accessed from outside of AWT, it's 
not a part of public API. If FX lived on Swing event dispatch thread, 
all the FX top-level windows (that are not embedded into JFXPanel) would 
never receive any events.

Thanks,

Artem

> One other possible solution that comes to my mind  and also a much simpler
> and quicker one is to add a Platform.invokeAndWait(like what exists on
> swing) alongside the Platform.runLater to the javafx API.
> And each time I want to invoke something on the javafx App thread from the
> swing ui thread I call invokeAndWait instead of runLater. So effectively
> the two UI threads become only one, because they are not running
> concurrently but sequentially instead.


More information about the openjfx-dev mailing list