Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaFX Scene Builder and fx:include

I am wondering how one can use fx:include in conjunction with the JavaFX Scene Builder, therefore:

Imagine I have a BorderPane (file borderpane.fxml). In the center section I want to put a Label which shall however be defined in a separate FXML file, e.g. label.fxml.

First problem of this: As the label.fxml will be integrated into a container (the BorderPane) it doesn't need one itself. The SceneBuilder however only offers the option to create layouts being a container?

Second problem: I can create the label.fxml manually and then adapt borderpane.fxml manually to include the label.fxml. I then can load the borderpane.fxml file using SceneBuilder without any problems. However when I now change the text of the label and choose "Save", not the label.fxml is modified, but instead the borderpane.fxml is modified like this:

# borderpane.fxml  
<fx:include source="label.fxml" text="the new label text" />

The new label text should be written to label.fxml, not to borderpane.fxml, what is currently done.

  • Am I doing something wrong?

  • Is SceneBuilder not intended to be used in conjunction with fx:include?

  • I want separate Controllers for parts of my GUI, I think this is quite logically/normal, so this practical usage scenario is hopefully somehow supported by the SceneBuilder?

like image 462
stefan.at.wpf Avatar asked Feb 09 '13 14:02

stefan.at.wpf


People also ask

What is FX ID in Scene Builder?

The fx:id is the identity associated to component in fxml to build a controller, and the id is used for css.

What is the use of Scene Builder in JavaFX?

Scene Builder allows you to easily layout JavaFX UI controls, charts, shapes, and containers, so that you can quickly prototype user interfaces. Animations and effects can be applied seamlessly for more sophisticated UIs.

What is FXML File in JavaFX?

FXML is an XML-based user interface markup language created by Oracle Corporation for defining the user interface of a JavaFX application. FXML presents an alternative to designing user interfaces using procedural code, and allows for abstracting program design from program logic.


2 Answers

It seems that Scene Builder 2 solved both the issue you mentioned.

First (solved) problem: the root node doesn't have to be a Pane. For example, it can be a Label

Second (solved) problem: The menu File > Include > FXML... can be used to include an external FXML file. It is not possible to edit the included file. Anyway, right-clicking the node in the Document view will show the "Edit included file" menu item, which opens another Scene Builder window for that file.

In conclusion, Scene Builder 2 correctly handles the fx:include element.

like image 73
Paolo Fulgoni Avatar answered Oct 31 '22 19:10

Paolo Fulgoni


I'm not aware of any method of adding nodes to embedded FXML contols using scene builder.

You can do this using Java code however. If your borderpane.fxml defined a controller with a method called #setCentre to add a node then you could load label.fxml in the parent controller of borderpane.fxml and add the label using the method you have defined.

One question though, are you giving a simplified example or are you actually creating FXML files for labels and border panes? If so then I think this will actually make your program more complex than just adding controls to a larger control.

like image 33
Andy Till Avatar answered Oct 31 '22 18:10

Andy Till