How to swap screens in a JavaFX application in the controller class?

后端 未结 3 1542

If there are 3 files in a JavaFX project; an FXML file, a controller for the FXML, and an application class; how can the controller respond to a button click (which works pe

3条回答
  •  猫巷女王i
    2020-11-29 07:20

    @FXML
    private void handleButtonAction(ActionEvent event) {
        System.out.println("You clicked me!");
        label.setText("Hello World!");
        //Here I want to swap the screen!
    
        Stage stageTheEventSourceNodeBelongs = (Stage) ((Node)event.getSource()).getScene().getWindow();
        // OR
        Stage stageTheLabelBelongs = (Stage) label.getScene().getWindow();
        // these two of them return the same stage
        // Swap screen
        stage.setScene(new Scene(new Pane()));
    }
    

提交回复
热议问题