Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaFx Nested Controllers (FXML <include>)

In this tutotial, is an example of how to include custom components and use their controllers from the controller of the container.

main_window_content.fxml

<VBox fx:controller="com.foo.MainController">    <fx:include fx:id="dialog" source="dialog.fxml"/>    ... </VBox> 

MainController.java

public class MainController extends Controller {     @FXML private Window dialog;     @FXML private DialogController dialogController;      .. 

If the component is included only once, it works fine. If the same component is included twice, the controllers are not initialized. Both controllers are null.

main_window_content.fxml

    <VBox fx:controller="com.foo.MainController">        <fx:include fx:id="dialog1" source="dialog.fxml"/>        <fx:include fx:id="dialog2" source="dialog.fxml"/>        ...     </VBox> 

MainController.java

    public class MainController extends Controller {         @FXML private Window dialog1;         @FXML private DialogController dialogController1;         @FXML private Window dialog2;         @FXML private DialogController dialogController2; 

Could someone help me to solve the problem? thanks

This is my FXML loading code. It is executed in main application method:

public void start(Stage stage) throws Exception {      Parent root = FXMLLoader.load(getClass().getResource("main_window_content.fxml"));     stage.setTitle("FXML Welcome");      stage.setScene(new Scene(root, 300, 275));     stage.show();  } 
like image 770
Antonello Avatar asked Sep 22 '12 11:09

Antonello


People also ask

Can two FXML have same controller?

It's possible to use the same controller class for multiple FXML files, but your code will be really hard to follow, and it's a very bad idea. (Also note that using the fx:controller attribute, you will have a different controller instance each time you call FXMLLoader.

How do controllers work in JavaFX using FXML?

JavaFX controller works based on MVC(Model-View-Controller) JavaFX MVC can be achieved by FXML (EFF-ects eXtended Markup Language). FXML is an XML based language used to develop the graphical user interfaces for JavaFX applications as in the HTML.

What is FX ID in FXML?

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

What does FXML loader do?

Class FXMLLoader. Loads an object hierarchy from an XML document.


2 Answers

Thanks to Daniel (from OTN) I found the error in my code, the names of my controller variables were wrong. They should be <fx:id>Controller. In other words it should be:

MainController.java

public class MainController extends Controller { @FXML private Window dialog1; @FXML private DialogController dialog1Controller; @FXML private Window dialog2; @FXML private DialogController dialog2Controller; 

But studying the changes introduced in version 2.2 I found that everything can be easily solved by using <fx:root> tag (like this tutorial). I entered my component in FXML simply declaring it like this:

<HBox>     <Dialog id="dialog1" text="Hello World!"/>     <Dialog id="dialog2" text="Hello World!"/> </HBox> 

I hope to be helpful

like image 117
Antonello Avatar answered Sep 28 '22 10:09

Antonello


There seems to be a bug in netbeans 8.0 with nested fxmls as well. Cannot count on netbeans to create the nested fxml's controller object for you, it has to be manually inserted into your MainController. Every time the controller is updated in netbeans it gets wiped out so it can be kind of tedious. For this example that would be inserting the

@FXML private DialogController dialog1Controller; 

line manually into the main controller in this case, then it works normally. Very useful for organizing large fxmls/controllers.

like image 45
SamHuman Avatar answered Sep 28 '22 10:09

SamHuman