Java FX从不同场景改变Label的值 [英] Java FX changing value of Label from different scene

查看:840
本文介绍了Java FX从不同场景改变Label的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个场景。第一个场景使用以下代码调用第二个场景。

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();
 }

Confirmation.fxml中有一个名为Proceed的标签。

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

我需要在此函数中更改该标签的内容并返回结果(true / false)。帮助?

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

推荐答案

为FXML创建一个 ConfirmationController 。从控制器,公开一个方法,允许您传递数据(字符串)以设置为标签。

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);
    }
    ...
}

在你的方法里面你正在加载FXML,你可以:

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

这篇关于Java FX从不同场景改变Label的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆