JavaFX 更新 textArea
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18597644/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JavaFX update textArea
提问by Genesist
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:
我有一个简单的 JavaFX 应用程序,它有一个 TextArea。我可以在 start() 方法中使用以下代码更新 textArea 的内容:
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();
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.
代码只是将text
字符串写入 TextArea 2000 次。我想从一个在 start() 方法之外实现的函数更新这个 textArea。
public void appendText(String p){
txtarea.appendText(p);
}
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 应用程序更新 TextArea 的任意程序调用此函数。如何在 appendText 函数中执行此操作?
采纳答案by nyyrikki
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.:
您可以为需要写入javafx.scene.control.TextArea
的类提供对包含该public void appendText(String p)
方法的类的引用,然后调用它。我建议您还传递一个指示该方法是从哪个类调用的,例如:
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");
}
}