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!
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);
}