JavaFX 使用控制器从另一个类更改标签文本 [英] JavaFX Change label text from another class with controller

查看:68
本文介绍了JavaFX 使用控制器从另一个类更改标签文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用另一个类的控制器更改标签的文本.我在 FXMLDocumentController 中做了一个方法,将文本设置为标签:

public void setLabelText(String text){lbZeit.setText(文本);}

现在我想从另一个类(例如我的 SerialHandlerClass)更改此文本.首先,我需要控制器,对吗?所以我这样做了:

FXMLLoader loader = new FXMLLoader(FXMLDocumentController.class.getResource("FXMLDocument.fxml"));loader.load();控制器 = (FXMLDocumentController) loader.getController();

现在我运行setLabelText"方法....

controller.setLabelText("asd");

...什么也没发生...

这很有趣,因为当我在setLabelText(String text)"方法中添加System.out.println(text);时,程序会将正确的文本写入控制台.>

但是,为什么?

抱歉我的英语不好,这不是我的母语:)

谢谢,朱利安

解决方案

您没有更新标签,因为您在使用 FXMLoader 时正在创建 FXMLDocumentController 的另一个实例.

您应该将包含标签的控制器实例设置为其他类的参数.

下面的代码可以解决您的需求.这里我将Controller实例设置为Connector类,这样就可以调用其他类的setLabelText方法:

公共类连接器{公共静态无效连接(FXMLDocumentController 控制器){尝试 {System.out.println("Connector.Connecting(): Called");controller.setLabelText("再见世界");} catch (IOException ex) {Logger.getLogger(Connector.class.getName()).log(Level.SEVERE, null, ex);}}}公共类 FXMLDocumentController 实现 Initializable {@FXML自有品牌标签;@FXML私有无效 handleButtonAction(ActionEvent 事件){System.out.println("FXMLDocumentController.#handleButtonAction");label.setText("Hello World!");Connector.Connecting(this);}@覆盖公共无效初始化(URL url,ResourceBundle rb){//去做}公共无效 setLabelText(字符串文本){System.out.println("FXMLDocumentController.setLabelText(): 调用");label.setText(文本);}}

注意:

如果你的例程需要更长的时间来执行它需要的任何东西,你可能想要使用任务,这样你就不会冻结你的 UI.要更新标签,您必须绑定文本属性,然后使用 updateMessage() 方法更新文本值.

公共类 FXMLDocumentController 实现 Initializable {@FXML自有品牌标签;@FXML私有无效 handleButtonAction(ActionEvent 事件){System.out.println("FXMLDocumentController.#handleButtonAction");label.setText("Hello World!");任务<布尔值>connectorTask = new ConnectorTask();label.textProperty().bind(connectorTask.messageProperty());connectorTask.setOnSucceeded(e -> {//如果任务结束没有错误,这将被调用label.textProperty().unbind();});新线程(连接器任务).开始();}@覆盖公共无效初始化(URL url,ResourceBundle rb){//去做}//public void setLabelText(String text)//{//System.out.println("FXMLDocumentController.setLabelText(): Called");//label.setText(text);//}公共类 ConnectorTask 扩展了 Task{@覆盖受保护的布尔调用()抛出异常{//... 在这里做任何你需要的事情//然后调用此方法从绑定的 Label 更新 TextProperty.updateMessage("再见世界");返回 Boolean.TRUE;}}}

注意:

此问题可能存在重复问题,请在此处查看我对此问题的回答!

I want to change the text of a Label with the controller from another class. I have made a method in the FXMLDocumentController, which sets the text to the label:

public void setLabelText(String text)
{
  lbZeit.setText(text);
}

Now I want to change this text from another class like my SerialHandlerClass. First, I need the controller, am I right? So I did this:

FXMLLoader loader = new FXMLLoader(FXMLDocumentController.class.getResource("FXMLDocument.fxml"));
loader.load();
controller = (FXMLDocumentController) loader.getController();

Now I run the "setLabelText" method....

controller.setLabelText("asd");

... and nothing happens...

It's very funny, because when I add System.out.println(text); to the "setLabelText(String text)" method, the program writes the correct text to the console.

But, why?

Sorry for my bad english, it's not my native language :)

Thanks, Julian

解决方案

You are not updating the label because you are creating another instance of FXMLDocumentController when you use the FXMLoader.

You should set the controller instance, that contains the label, as a parameter to the other Class.

Below you have the code that could solve your need. Here I set the Controller instance to the Connector class, so you can call the setLabelText method from the other class:

public class Connector {
    public static void Connecting(FXMLDocumentController controller) {
        try {
            System.out.println("Connector.Connecting(): Called");
            controller.setLabelText("Bye World");
        } catch (IOException ex) {
            Logger.getLogger(Connector.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}


public class FXMLDocumentController implements Initializable {

    @FXML
    private Label label;

    @FXML
    private void handleButtonAction(ActionEvent event) {
        System.out.println("FXMLDocumentController.#handleButtonAction");
        label.setText("Hello World!");
        Connector.Connecting(this);
    }

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO
    }    

    public void setLabelText(String text)
    {
        System.out.println("FXMLDocumentController.setLabelText(): Called");
        label.setText(text);
    }

}

Note:

If your routine is going to take longer to execute whatever it needs to, you might want to use a Task, so you don't freeze your UI. To update the Label, you have to bind the text property and then update the Text value using the updateMessage() method.

public class FXMLDocumentController implements Initializable {

    @FXML
    private Label label;

    @FXML
    private void handleButtonAction(ActionEvent event) {
        System.out.println("FXMLDocumentController.#handleButtonAction");
        label.setText("Hello World!");

        Task<Boolean> connectorTask = new ConnectorTask();
        label.textProperty().bind(connectorTask.messageProperty());
        connectorTask.setOnSucceeded(e -> {
            // this is going to be called if the task ends up without error
            label.textProperty().unbind();
        });
        new Thread(connectorTask).start();
    }

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO
    }    

    //public void setLabelText(String text)
    //{
    //    System.out.println("FXMLDocumentController.setLabelText(): Called");
    //    label.setText(text);
    //}


    public class ConnectorTask extends Task<Boolean> {

        @Override
        protected Boolean call() throws Exception {
            // ... do whatever you need here

            // then you call this method to update the TextProperty from the Label that was bound.
            updateMessage("Bye World");

            return Boolean.TRUE;
        }
    }

}

NOTE:

There is a possible duplicate question for this, please see my answer for this question here!

这篇关于JavaFX 使用控制器从另一个类更改标签文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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