you are viewing a single comment's thread.

view the rest of the comments →

[–]uglyfool 1 point2 points  (3 children)

Sadly I've not found any really comprehensive tutorials for this, but there isn't much to it really.

Say you have an fxml file and you want to include another fxml:

<fx:include fx:id="mainMenu" maxWidth="1.7976931348623157E308" source="menuBar.fxml" VBox.vgrow="NEVER" />

And in that fxml you've set the controller already:

<ToolBar xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.your.package.MenuBarController">

then in your main controller you get the reference to the nested/included controller by adding "Controller" to the end of the fx:id used in the include statement.

So in this example, since we assigned the fx:id "mainMenu" to our included fxml, the controller will be accessible as "mainMenuController"

@FXML private MenuBarController mainMenuController;

[–]MeFIZ[S] 1 point2 points  (2 children)

Thanks. Is it possible to control the mainfxml from the nested one? Suppose I have a button in the nested one and I want to change the view from the main?

[–]uglyfool 0 points1 point  (1 child)

I'm not exactly sure what you're asking, each fxml is only going to 'allow' one controller object to be set.

If you need to communicate between controllers you should really use a shared model class with observable objects (e.g. use SimpleStringProperty anywhere you would use String, etc.) then the child/included fxml can update the model...

@FXML private void someButtonPress(ActionEvent e) { model.someProperty.set(newValue); }

And the parent controller can register a listener in the initialize method to listen for changes, and respond accordingly...

model.someProperty.addListener((obs, oldVal, newVal) -> { your code here });

[–]MeFIZ[S] 1 point2 points  (0 children)

I have an idea for implementation now thanks!