Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
537 views
in Technique[技术] by (71.8m points)

javafx - Java FX changing value of Label from different scene

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

@FXML
private void confirmation(ActionEvent event) throws IOException{
 Stage confirmation_stage;
 Parent confirmation;
 confirmation_stage=new Stage();
 confirmation=FXMLLoader.load(getClass().getResource("Confirmation.fxml"));
 confirmation_stage.setScene(new Scene(confirmation));
 confirmation_stage.initOwner(generate_button.getScene().getWindow());
 confirmation_stage.show();
 }

There is a label in "Confirmation.fxml" called "Proceed".

I need to change the content of that label from within this function and return the result(true/false). Help?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...