Integrating JFX Dialog/Stage in Swing application

Jeff Martin jeff at reportmill.com
Sat May 31 13:57:34 UTC 2014


I'm sure this isn't the proper answer, but I have used this call to initialize the FX toolkit on demand from various Swing contexts and it has always worked for me:

	new javafx.embed.swing.JFXPanel();

Then you would need the Platform.runLater(). Usually for the whole method that creates your UI and shows the Stage. In some cases, I would make the first line of my method that ends up invoking the JFX dialog something like this:

public void doSomething()
{
	// Ensure we're on FX thread
	if(!Platform.isFXApplicationThread()) {
		Platform.runLater(new Runnable() { public void run() { doSomething(); } return; }

	… <create FX UI and do stage.show()> ...
}

I've done quite a bit of this and it works without problems (for me).

jeff martin

On May 31, 2014, at 7:27 AM, Robert Krüger <krueger at lesspain.de> wrote:

> Hi,
> 
> I am trying something which I thought would technically be the easiest
> way of migrating parts of an existing application from Swing to JFX,
> i.e. have a Swing JMenuItem trigger the showing of a JFX stage because
> I thought this would technically even be cleaner than to have a swing
> dialog containing an JFXPanel.
> 
> Doing this results in the following Exception:
> 
> java.lang.IllegalStateException: Toolkit not initialized
> at com.sun.javafx.application.PlatformImpl.runLater(PlatformImpl.java:276)
> ~[jfxrt.jar:na]
> at com.sun.javafx.application.PlatformImpl.runLater(PlatformImpl.java:271)
> ~[jfxrt.jar:na]
> at javafx.application.Platform.runLater(Platform.java:78) ~[jfxrt.jar:na]
> at de.lesspain.mediatool.menu.ToolsSubmenu$1.actionPerformed(ToolsSubmenu.java:20)
> ~[
> 
> javadoc of runLater states:
> 
> This method must not be called before the FX runtime has been
> initialized. For standard JavaFX applications that extend Application,
> and use either the Java launcher or one of the launch methods in the
> Application class to launch the application, the FX runtime is
> initialized by the launcher before the Application class is loaded.
> For Swing applications that use JFXPanel to display FX content, the FX
> runtime is initialized when the first JFXPanel instance is
> constructed.
> 
> So this is consistent. Still I am wondering, why it should not be
> supported to just trigger opening a stage from a Swing menu? Either by
> Platform.runLater autoinitializing or offering a separate method like
> Platform.ensureInitialized().
> 
> Am I missing something obvious?
> 
> Thanks,
> 
> Robert



More information about the openjfx-dev mailing list