javafxpropertieslistenerspanes

How do I make different panes visible when button selected?


So here is the problem I am having: I have 3 button, when I press one of the 3 buttons the new scene will display. Yet I cant seem to figure out how to make different panes appear visible depending on which button is pressed. what is the best method to perform this action? How do i use Id's from different scene controllers in order to change the properties of the pane within the main scene button listener?

currently on my main controller when each button is released the below action listener executes and displays secondscreen.fxml. the secondscreen.fxml has 2 different panes, depending on which button is pressed I need 1 of the 2 pains to set to visable.

@FXML
 public void handleButtonAction(MouseEvent event) {
    Parent root;
    try {
        root = FXMLLoader.load(getClass().getResource("secondscreen.fxml"));
        Stage stage = new Stage();
        stage.setTitle("title");
        stage.setScene(new Scene(root));
        stage.show();

        ((Node)(event.getSource())).getScene().getWindow();

    } catch (Exception e) {
        e.printStackTrace();
    }
}

Solution

  • After you load a view, you can access its controler.

    Keep in mind, that your controller must be asigned to your fxml file fx:controller="your.package.SecondScreenController.java"

    FXMLLoader loader = new FXMLLoader.load(getClass().getResource("secondscreen.fxml"));
    // load view
    Parent root = loader.load();
    // after view is loaded, access its controller
    SecondScreenController controller = (SecondScreenController) loader.getController();
    // modify view using methods on your controller
    controller.setTabIndex(0)
    
    Stage stage = new Stage();
    stage.setScene(new Scene(root));
    stage.show();