JavaFX更新textArea [英] JavaFX update textArea

查看:506
本文介绍了JavaFX更新textArea的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的JavaFX应用程序,它有一个TextArea。我可以使用start()方法中的以下代码更新textArea的内容:

I have a simple JavaFX application which has a TextArea. I can update the content of the textArea with the code below inside the start() method:

new Thread(new Runnable() {

    public void run() {

        for (int i = 0; i < 2000; i++) { 

            Platform.runLater(new Runnable() {
                public void run() {
                    txtarea.appendText("text\n");
                }
            });
        }
    }
}).start();

代码只需将 text 字符串写入TextArea 2000次。我想从一个在start()方法之外实现的函数更新this textArea。

The code just write the text string into the TextArea 2000 times. I want to update this textArea from a function which is implemented outside of the start() method.

public void appendText(String p){
    txtarea.appendText(p);
}

可以从使用JavaFX应用程序更新的任意程序调用此函数文本区。如何在appendText函数中执行此操作?

This function can be called from arbitrary programs which use the JavaFX application to update the TextArea. How can I do this inside the appendText function?

推荐答案

您可以将需要写入的类提供给 javafx.scene.control.TextArea 对您的类的引用,其中包含 public void appendText(String p)方法,然后只需调用它。我建议你也传递一个指示调用该方法的类,例如:

You could give the class which needs to write to the javafx.scene.control.TextArea an reference to your class which holds the public void appendText(String p) method and then just call it. I would suggest you also pass an indication from which class the method was called, e.g.:

public class MainClass implements Initializable {
    @FXML
    private TextArea txtLoggingWindow;
    [...more code here...]
    public void appendText(String string, String string2) {
       txtLoggingWindow.appendText("[" + string + "] - " + string2 + "\n");
    }
}

public class SecondClass {
    private MainClass main;
    public SecondClass(MainClass mClass) {
        this.main = mClass;
    }
    public void callMainAndWriteToArea() {
        this.main.appendText(this.getClass().getCanonicalName(), "This Text Goes To TextArea");
    }
}

这篇关于JavaFX更新textArea的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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