2

I have a JavaFX 8 application...

MainApp.java

    Parent root;
    root = FXMLLoader.load(getClass().getResource("/fxml/MainForm.fxml"));
    Scene scene = new Scene(root);        
    stage.setScene(scene);
    stage.show();

The MainForm.fxml defines its controller with fx:controller="MainController" and the controller itself contains a text field textFieldUsername.

@FXML
protected TextField textFieldUsername;

Then, there is a second form AuxForm.fxml with another controller fx:controller="AuxController". This second form is included in the MainForm.fxml like this:

<content>
  <fx:include source="AuxForm.fxml" />
</content>

Now I need to get the value of textFieldUsername. This value is needed in the second controller but I have no idea how to do this. My first idea was public class AuxController extends MainController to have all controls available but this doesn't work.

Robert Strauch
  • 12,055
  • 24
  • 120
  • 192

1 Answers1

2

Add a fx:id to the fx:include:

<content>
  <fx:include fx:id="auxForm" source="AuxForm.fxml" />
</content>

Now you can inject the controller for the included fxml (the "nested controller") into the "main controller". Assuming AuxForm.fxml has fx:controller="AuxController" you can do:

public class MainController {

    @FXML
    private AuxController auxFormController ;


    public void initialize() {
        // call any methods you need on auxFormController ...

        // ...
    }

    // ...
}

The rule here is that you append the word Controller to the value of the fx:id to form the field name for the "nested controller".

Now you can define any methods you need in AuxController (e.g. public String getUsername() {...}) and invoke them from the main controller when you need.

For example, if you needed to provide data to the AuxController, you would provide a property there which you could set or bind from the MainController:

public class AuxController {

     private final StringProperty username ;

    public StringProperty usernameProperty() {
        return username ;
    }

    //...
}

Then in the main controller's initialize() method you can do

auxFormController.usernameProperty().bind(
    textFieldUsername.textProperty());

Now username.get() will give you the text in the text field.

See the FXML documentation for more details.

James_D
  • 201,275
  • 16
  • 291
  • 322
  • Thanks James for your posting. However I think I actually don't get it. In my example I'd like to use the `textFieldUsername` (defined in `MainController`) in a method defined in the `AuxController`. As far as I get your answer it's the other way round? – Robert Strauch Apr 20 '16 at 21:02
  • You shouldn't ever expose UI elements outside your controller. What do you need the text field for in the other controller? Elaborate on what you are trying to do and I'll update the answer tomorrow when I am back at the computer. – James_D Apr 20 '16 at 21:10
  • I have edited a part of my question (last paragraph) to: "Now I need to get the value of textFieldUsername. This value is needed in the second controller..." – Robert Strauch Apr 20 '16 at 21:26
  • Perfect, I just came across a similar idea and now it works like charm. Thank you. – Robert Strauch Apr 20 '16 at 21:57