6

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?

stefan.at.kotlin
  • 15,347
  • 38
  • 147
  • 270

2 Answers2

8

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.

Paolo Fulgoni
  • 5,208
  • 3
  • 39
  • 55
  • Nice explanation but there is one problem: if you include the fxml file you can't drag it in scence builder to put it wherever you want in your application! – SlimenTN Jul 18 '15 at 15:15
1

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.

Andy Till
  • 3,371
  • 2
  • 18
  • 23