JTextArea不动态更新 [英] JTextArea not updating dynamically
问题描述
我想在动态更新的类中有一个JTextArea。目前它只显示我在完成所有处理后附加到它的文本。我试图实现以下内容来解决它:
I have a JTextArea inside a class that I want to update dynamically. Currently it is only displaying the text I append to it after all the processing is done. I have tried to implement the following to fix it:
public NewConsole(){
initComponents();
}
public void write(final String s){
SwingUtilities.invokeLater(new Runnable() {
public void run() {
textarea.append(s);
}
});
}
控制台在父类中实例化为:
Console gets instantiated in a parent class as:
protected NewConsole console = new NewConsole();
并输出到所有孩子的电话:
and to output to it, all the children call:
console.write("Append this..");
编辑:这里有更多信息:
Here's some more information:
public abstract class Parent{
protected NewConsole console = new NewConsole();
public Parent(){}
protected abstract int doSomething();
}
public class Child extends Parent{
public Child(){
console.write("I want this to update dynamically");
doSomething();
console.write("And this..");
}
public int doSomething(){
//Quite intensive processing here
}
}
推荐答案
在 doSomething
中进行的密集处理阻止了 EDT
,阻止了UI更新。请改用 SwingWorker 来执行此功能。
The intensive processing done in doSomething
is blocking the EDT
, preventing UI updates. Use a SwingWorker instead to perform this functionality.
使用执行以启动工作人员。将任何所需的调用移动到 console.write
到 doInBackground 或已完成。
Use execute to start the worker. Move any required calls to console.write
to either doInBackground or done.
这篇关于JTextArea不动态更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!