1

I am using JavaFX in Swing application, with JFXPanel. I have been coding JavaFX UI manually, with css files. I am using NetBeans 8.1.

I am wondering, in this case, can I use JavaFX Scene Builder to generate UI? AFAIK, the output is FXML file which represents UI components. Is this compatible with JFXPanel way of using JavaFX?

Thanks!

Shuwn Yuan Tee
  • 5,578
  • 6
  • 28
  • 42

1 Answers1

1

If you want to manage the JFXPanel itself, and the Swing components, in Scene Builder, then the short answer is "No".

From a purely practical perspective, SceneBuilder doesn't support Swing components, which is what you are going to be adding to your JFXPanel.

The other issue is to do with threading. Scene Builder simply generates FXML. The FXML is loaded by an FXMLLoader via one of its load(...) methods. That method must necessarily be executed on a single thread. If you mix Swing components and JavaFX components, you must manage the threads appropriately, as described in the JFXPanel documentation. Since you have no way of telling the FXMLLoader which parts of the FXML to process on the JavaFX Application Thread, and which parts to process on the AWT Event Dispatch thread, there is no way to load an FXML file describing a mix of Swing and JavaFX components that obeys the threading rules of both toolkits.

Obviously you can use Scene Builder just to manage the content of the JFXPanel; this is identical to the "regular" usage of Scene Builder. Again, all Scene Builder does is generate FXML, and FXML is simply a description of what objects to create and how they relate to each other. So you can just do

private JFXPanel jfxPanel ;

// build Swing components, initialize jfxPanel, etc

// run on FX Application Thread:
private void initFX() {

    FXMLLoader loader = new FXMLLoader(getClass().getResource("/path/to/fxml/file"));
    Parent root = loader.load();
    Scene scene = new Scene(root);
    jfxPanel.setScene(scene);
}
James_D
  • 201,275
  • 16
  • 291
  • 322
  • Because the application is written in Swing, it is huge & not an easy task to migrate to JavaFX. Hence for newly added UI, I started writing in JavaFX, using JFXPanel. With SceneBuilder, is it possible to only include part of UI, which is the newly added JavaFX UI? If yes, then I don't need to design JavaFX components in code & css, which is time consuming. – Shuwn Yuan Tee Oct 12 '16 at 08:58
  • OK, I understood from your question that you wanted to create/edit the `JFXPanel` and other Swing components in Scene Builder too. If all you want is to use Scene Builder for the content of the `JFXPanel`, then of course you can do that: it is exactly the same as the way you would normally use it. – James_D Oct 12 '16 at 11:37