The subcontroller is not entered into the main controller

I have a BorderPane (associated with MainController), FXML for BorderPane uses <fx:include> to include the label (with the StatusBarController) in the bottom area of ​​the BorderPane. Unfortunately, the StatusBarController is not injected into an instance of the MainController class, and I do not understand why.

main.fxml: BorderPane with the status bar turned on

 <fx:root type="javafx.scene.layout.BorderPane" fx:id="borderPane" xmlns:fx="http://javafx.com/fxml" fx:controller="com.example.controllers.MainController"> <bottom> <fx:include source="statusbar.fxml" /> </bottom> </fx:root> 

statusbar.fxml: label and associated controller

 <Label fx:id="statusbar" text="A label simulating a status bar" xmlns:fx="http://javafx.com/fxml" fx:controller="com.example.controllers.StatusBarController" /> 

MainController with the StatusBarController field:

 public class MainController { @FXML private StatusBarController statusbarController; // PROBLEM HERE: I would expect the StatusBarController to be injected but this does not happen! // Implementing Initializable Interface no longer required on JavaFX 2.2 according to // http://docs.oracle.com/javafx/2/fxml_get_started/whats_new2.htm // (I also tested this, the initialize() method is being called) @SuppressWarnings("unused") // only called by the FXMLLoader @FXML // This method is called by the FXMLLoader when initialization is complete private void initialize() { // initialize your logic here: all @FXML variables will have been injected assert borderPane != null : "fx:id=\"borderPane\" was not injected: check your FXML file 'main.fxml'."; System.out.println("MainController initialize()"); //statusbarController.setStatusText("Hello from MainController"); // PROBLEM HERE: this fails because statusbarController is not injected as expected } } 

And the beginning of the application:

 public void start(Stage primaryStage) { Parent root = null; try { root = FXMLLoader.load(getClass().getResource("/resources/main.fxml")); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } primaryStage.setTitle("Demo"); primaryStage.setScene(new Scene(root, 800, 600)); primaryStage.show(); } 

The full source code for my example is available at http://codestefan.googlecode.com/svn/trunk/SubcontrollerAccess/

So the question is: why is the StatusBarController not being entered into the statusbarController MainController variable?

Thanks for any hint!

+8
javafx-2 controller code-injection fxml
source share
2 answers

To use the @FXML tag, you must specify fx:id .

Update your main.fxml :

 <bottom> <fx:include fx:id="statusbar" source="statusbar.fxml" /> </bottom> 

After that, you can use the following constants in MainController.java :

 @FXML Label statusbar; // node itself @FXML private StatusBarController statusbarController; // controller 

Note that statusbarController not an incomplete class name, but fx:id + Controller .

+15
source share

There seems to be a bug in netbeans 8.0 with nested fxmls. It is not possible to rely on netbeans to create an embedded fxml controller object for you, you must manually insert it into your MainController. Each time the controller is updated in netbeans, it is destroyed, so it can be tedious. For this example, which will embed

@FXML private StatusBarController statusbarController;

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

0
source share

All Articles