JavaFX pass values from child to parent

后端 未结 2 1692
后悔当初
后悔当初 2020-12-06 04:00

I have one parent controller which contain one button. When I click on button it open new window and show some data into table. The code I have used for opening window is

2条回答
  •  借酒劲吻你
    2020-12-06 04:19

    In my example below, the account window (child/dialog) passes the newly selected account name back to the parent window when the save button is clicked. On the parent window ...

    Stage nstage = new Stage();
                    FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("Accounts.fxml"));
                    Parent root = (Parent) fxmlLoader.load();
                    AccountsController accController = fxmlLoader.getController();
                    accController.dialogMode(); //Call a function in acconunt windoow to preset some fields
    
                    Scene scene = new Scene(root);
                    nstage.setTitle("Create Company Account");
                    nstage.setScene(scene);
    
                    Stage stage  = (Stage) src.getScene().getWindow();
                    nstage.initOwner(stage);
                    nstage.initModality(Modality.APPLICATION_MODAL);
                    //nstage.initStyle(StageStyle.UNDECORATED); 
                    nstage.show();
    
                    nstage.setOnCloseRequest(new EventHandler(){
                        public void handle(WindowEvent we) {
                                txtCompany.setText( accController.lbl1.getText() );
                        }
    
                    });
    

提交回复
热议问题