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

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

问题描述

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

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



现在我想从另一个类,如我的SerialHandlerClass更改这个文本。首先,我需要控制器,是吗?所以我这样做:

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



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

  controller.setLabelText(asd); 

...没有任何反应...



这很有趣,因为当我在方法中添加 System.out.println(text);



对不起,我的英语不好,这不是我的母语: )



感谢,
Julian

解决方案

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



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



下面你有可以解决你的需要的代码。这里我将Controller实例设置为Connector类,因此您可以从其他类调用 setLabelText 方法:

  public class Connector {
public static void连接(FXMLDocumentController控制器){
try {
System.out.println(Connector.Connecting称为);
controller.setLabelText(Bye World);
} catch(IOException ex){
Logger.getLogger(Connector.class.getName())。log(Level.SEVERE,null,ex);
}
}
}


public class FXMLDocumentController implements可初始化{

@FXML
私有标签;

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

}

strong>



如果你的例程需要更长的时间来执行它所需要的任何东西,你可能想使用一个任务,所以你不冻结你的UI。要更新Label,您必须绑定text属性,然后使用 updateMessage()方法更新Text值。

  public class FXMLDocumentController implements可初始化{

@FXML
私有标签标签;

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

任务< Boolean> connectorTask = new ConnectorTask();
label.textProperty()。bind(connectorTask.messageProperty());
connectorTask.setOnSucceeded(e - > {
//如果任务结束没有错误,这将被调用
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 {
// ...在此处执行任何操作

//然后调用这个方法从被绑定的Label中更新TextProperty。
updateMessage(Bye World);

return 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天全站免登陆