0

Let's assume, for simplicity's sake, I have an FXML with a SplitPane. On one side I have a Button; the other has an empty AnchorPane (for now).

The button simply places a pane on the other side of the SplitPane from another FXML file. The newly added pane contains a Button and a Label. Both FXMLs have the same controller for two reasons: 1. The Added Pane FXML is not used elsewhere, 2. The Added Pane FXML has so little functions that it's not really worth it to have a separate controller for it.

Pressing the button on the newly added pane sets the text in its label to "Hello World".

Split Pane

<SplitPane fx:controller="myController".... >
    <items>
       <VBox .... >
           <Button fx:id="mainButton" onAction="#handleFirstButton".... />
       </VBox>
       <AnchorPane fx:id="secondPane" ....>
    </items>
</SplitPane>

Added Pane

<VBox fx:controller="myController" ....>
    <Button fx:id="btn" onAction="#changeText" ... />
    <Label fx:id="lbl" .... />
</VBox>

Controller Class

// Regular stuff here

    @FXML
    AnchorPane secondPane;

    @FXML
    Button mainButton;

    @FXML
    Button btn;

    @FXML
    Label lbl;

    public void initialize(URL arg0, ResourceBundle arg1) {
    }

    private void loadSecondPane {
        FXMLLoader loader = FXMLLoader.load(getClass().getResource("AddedPane.fxml"));
        VBox box = loader.load();

        secondPane.setTopAnchor(box);    
    }

    @FXML
    private void handleFirstButton(ActionEvent event) {
        loadSecondPane();
    }

    @FXML
    private void changeText(ActionEvent event) {
        lbl.setText("Hello World");
    }

The problem is, when the Added Pane is loaded, its components will NOT be defined in the controller, and trying to perform any method on them will produce a NullPointerException.

Is there a way around this or is having a separate controller mandatory in this case? And assuming I have 2+ buttons on the first side of the pane, and each button produces a different pane on the other side, does the solution remain the same?

NOTE: I'm fairly new with javafx, so excuse any mistypes.

hman_codes
  • 794
  • 9
  • 24
  • Reusing the same controller class is not wrong per se, but it's an indicator of failing to seperate the concerns... Note that each call of `FXMLLoader.load` for a fxml containing the `fx:controller` attribute creates ***a new instance of the controller class***.It would be better to do "inter-right-hand content communication" through the model and designing an interface common to all the controllers for the sub-fxmls. The following question should help you with properly establishing "communication" with the controllers https://stackoverflow.com/questions/14187963/passing-parameters-javafx-fxml – fabian Oct 13 '18 at 09:03
  • @fabian I already saw that thread, but he talks of building a whole window/stage; I only want to display new content in my current window, so I thought there might be a different approach. However, I tried passing the new controller data from the main one, but I ended up with a `NullPointerException`. The exception referred to the data I'm trying to pass. But I guess this issue is for another thread/question. – hman_codes Oct 13 '18 at 09:53

0 Answers0