Java 在处理发生时动态刷新 JTextArea?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/629315/
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
Dynamically refresh JTextArea as processing occurs?
提问by digiarnie
I am trying to create a very simple Swing UI that logs information onto the screen via a JTextArea as processing occurs in the background. When the user clicks a button, I want each call to:
我正在尝试创建一个非常简单的 Swing UI,当处理发生在后台时,它通过 JTextArea 将信息记录到屏幕上。当用户单击按钮时,我希望每次调用:
textArea.append(someString + "\n");
to immediately show up in the UI.
立即显示在 UI 中。
At the moment, the JTextArea does not show all log information until the processing has completed after clicking the button. How can I get it to refresh dynamically?
目前,在单击按钮后处理完成之前,JTextArea 不会显示所有日志信息。我怎样才能让它动态刷新?
采纳答案by Ascalonian
I ran into the same issue with my application. I had a "Run" button my application that performed some actions and outputted the results to a JTextArea. I had to call the method from a Thread. Here is what I did.
我的应用程序遇到了同样的问题。我的应用程序有一个“运行”按钮,可以执行一些操作并将结果输出到 JTextArea。我不得不从线程调用该方法。这是我所做的。
I have several radio buttons of actions that can be done, and then one "Run" button to execute that particular action. I have an action called Validate. So when I check that radio button and click the "Run" button, it calls the method validate(). So I first placed this method into an inner class that implemented Runnable
我有几个可以完成的操作的单选按钮,然后是一个“运行”按钮来执行该特定操作。我有一个名为验证的操作。因此,当我检查该单选按钮并单击“运行”按钮时,它会调用方法 validate()。所以我先把这个方法放到一个实现了Runnable的内部类中
class ValidateThread implements Runnable {
public void run() {
validate();
}
}
I then called this thread in the ActionListener of the "Run" button as so
然后我在“运行”按钮的 ActionListener 中调用了这个线程
runButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
// Some code checked on some radio buttons
if(radioButton.isSelected()) {
if(radioButton.getText().equals("VALIDATE")) {
Runnable runnable = new ValidateThread();
Thread thread = new Thread(runnable);
thread.start();
}
}
}
});
Voila! The output is now sent to the JTextArea.
瞧!输出现在发送到 JTextArea。
Now, you will notice that the JTextArea will not scroll down with the text. So you need to set the caret position like
现在,您会注意到 JTextArea 不会随着文本向下滚动。所以你需要设置插入符号位置
textArea.setCaretPosition(textArea.getText().length() - 1);
Now when the data is added to the JTextArea, it will always scroll down.
现在,当数据添加到 JTextArea 时,它将始终向下滚动。
回答by Spencer Ruport
You'll need multithreading for this. Ready to take the plunge?
为此,您将需要多线程。准备好冒险了吗?
回答by binarycreations
If it's multithreading you need try this! http://www.devarticles.com/c/a/Java/Multithreading-in-Java/
如果是多线程,你需要试试这个! http://www.devarticles.com/c/a/Java/Multithreading-in-Java/
回答by Mark
As others have said this requires multithreading. Have a look at Concurrency in Swing.
One solution would be to implement the processing using a SwingWorker. The doInBackground method would implement the processing and you would invoke the publish method with the String to be appended as the argument. Your SwingWorker would then override the process method to take a String argument and append it to the text area.
一种解决方案是使用 SwingWorker 实现处理。doInBackground 方法将实现处理,您将使用要附加的字符串作为参数调用发布方法。然后,您的 SwingWorker 将覆盖 process 方法以获取 String 参数并将其附加到文本区域。
回答by petr
Try this:
尝试这个:
jTextArea.update(jTextArea.getGraphics());
回答by collielimabean
I'm sorry for replying to this question that was posted 4 years ago, but I have another solution that worked for me. I just used pointers to update the JTextArea
as such:
我很抱歉回答了 4 年前发布的这个问题,但我有另一个对我有用的解决方案。我只是使用指针来更新JTextArea
这样的:
//JScrollPane variable pane initialized with JTextArea area
//We will update area with new text
JTextArea temp = (JTextArea) pane.getViewPort().getView();
//new text to add
JTextArea c = new JTextArea();
c.append("text \n)";
//update through pointers
temp = c;
pane.validate();