1

we are wroking on a project including both javafx and javaswing. We have a main menu which is made by Java Swing. We need to launch JavaFX applications when a swing button is hit from our main menu. Our JavaFX part of the program is 2400 lines so we cannot convert it to JFXPanel. I'm already experienced with JavaFX up to a point. But I cannot solve this problem. Any help will be appreciated. Thank you.

  • 4
    Please be a bit more specific. Why can't you split up your JavaFX part into several JFXPanels? What has the number of lines to do with that? – mipa May 09 '22 at 18:17
  • Why not write the “main menu” in JavaFX? – James_D May 09 '22 at 19:16

1 Answers1

2

The main recommendation is not to mix Swing and JavaFX unless you really need to. In general, it is best to use a single toolkit for the UI rather than mix them.

Using a JFXPanel (recommended)

Follow the javafx swing integration documentation, which explains how to write the code.

The documentation I linked is a little outdated. The code remains the same, but JavaFX was modularized and removed from most JDK distributions from JDK/JavaFX 11 on, so you need to do some additional work.

Applying a JFXPanel to an existing JavaFX application

The javafx swing integration documentation I linked is for putting the JavaFX content in a swing panel (JFXPanel) but I noted that you say you cannot use such a panel in your application. I am not quite sure why you can't do that. JFXPanel is just a generic swing component that can hold any JavaFX scene. The linked javadoc also shows how to use it.

To apply a JFXPanel to an existing JavaFX application, have the JavaFX application supply a method that provides the scene it would normally set on the primary stage in the application start method. Call your new method to get the scene to embed it in your JFXPanel inside a frame of the Swing application. You don't need to call launch on the JavaFX application with this method.

Getting JavaFX components and Modularity

To work with a modular JavaFX, also follow the instructions at openjfx.io:

  • The maven dependency required is org.openjfx:javafx-swing:<version>

    <dependency>
        <groupId>org.openjfx</groupId>
        <artifactId>javafx-swing</artifactId>
        <version>18</version>
    </dependency>
    
  • Module info (if you need it) is:

    requires javafx.swing;
    

Module info is only required if you are writing a modular application that provides module-info.java, otherwise you need to add the module to the module path for compilation and runtime via command-line switches (as outlined in the openjfx.io documentation linked).

Or, you can rely on a JDK which includes both JavaFX and swing modules in the distribution and places them on the boot module path, e.g.:

Launching a JavaFX application from Swing (not recommended)

Alternately you could possibly invoke the JavaFX application launch method directly.

If you do so, study the application lifecycle documentation on the linked javadoc.

You can only call launch once and it will block the calling thread until the JavaFX app exits.

If you want multiple, non-blocking calls you will need to do some work to use such an approach. For example, spawn a separate execution thread to launch the application (so that you don't block the Swing thread). Once the JavaFX application is launched, use Platform.runLater to communicate with the JavaFX application from Swing as needed (for example to request the JavaFX application to show or hide).

If you want to call launch more than once (which you can't do in a single JVM instance), then, for an alternative, see the technique in:

If you are working with a modern JDK and JavaFX distribution, then module setup requirements are the same for this approach as the JFXPanel approach.

I recommend using a JFXPanel rather than launching a JavaFX application from Swing.

Swing in JavaFX

If you wish to go another route and embed swing content inside a JavaFX application (for example, as suggested by James_D in the comments, "write the “main menu” in JavaFX"), then you can use a SwingNode, which is a JavaFX node to be displayed in a JavaFX application which can contain swing components:

jewelsea
  • 150,031
  • 14
  • 366
  • 406