Java FX changing value of Label from different scene

后端 未结 3 467
孤街浪徒
孤街浪徒 2021-01-24 12:39

I have two scenes. The first scene invokes second scene using the following code.

@FXML
private void confirmation(ActionEvent event) throws IOException{
 Stage c         


        
3条回答
  •  执念已碎
    2021-01-24 13:12

    Create a ConfirmationController for the FXML. From the controller, expose a method which allows you to pass data (string) to set to the label.

    public class ConfirmationController implements Initializable {
    
        ...
        @FXML
        private Label proceed;
        ...
        public void setTextToLabel (String text) {
             proceed.setText(text);
        }
        ...
    }
    

    Inside your method where you are loading the FXML, you can have :

    ...
    FXMLLoader loader = new FXMLLoader(getClass().getResource("Confirmation.fxml"));
    confirmation = loader.load();
    ConfirmationController controller = (ConfirmationController)loader.getController();
    controller.setTextToLabel("Your Text"); // Call the method we wrote before
    ...
    

提交回复
热议问题