Java FX changing value of Label from different scene

蓝咒 提交于 2019-12-02 02:53:27

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
...

Labels in FXML have a setText method. So for your case the "Proceed" label will look something like:

Proceed.setText("The new text");

As for the second part of the question, I'm not 100% sure as to what you are asking. I don't really see any case for the function to return true or false.

Assuming that you have a controller called:confirmation_controller.java'. inside that controller, you have a public method getProceedLabel() that returns a reference for the label called Proceed. you can try the following code:

 Stage confirmation_stage;
 Parent confirmation;
 confirmation_stage=new Stage();
 FXMLLoader loader = new FXMLLoader(getClass().getResource("Confirmation.fxml"));
 confirmation = loader.load();
 confirmation_controller controller = loader.getController();
 Label label = controller.getProceedLabel();
 label.setText("..."):
 confirmation_stage.setScene(new Scene(confirmation));
 confirmation_stage.initOwner(generate_button.getScene().getWindow());
 confirmation_stage.show();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!