简体   繁体   English

JtextArea更新问题不实时更新

[英]JtextArea update issue not updating real time

I'm currently writing a application, which on a mouse click runs several methods which updates a JtextArea. 我当前正在编写一个应用程序,单击鼠标即可运行几种更新JtextArea的方法。 The problem is even though I'm updating the text area with each method call, it doesn't actually update until everything in the mouseclick has run.. 问题是,即使我使用每个方法调用都更新了文本区域,它实际上并没有更新,直到mouseclick中的所有内容都已运行。

This can take quite a while to run through everything and I would like to see the text area update with each call instead of waiting until everything is done 这可能需要一段时间才能完成所有工作,我希望每次调用都能看到文本区域更新,而不必等到一切完成为止

public void mouseClicked(MouseEvent e) {
                DataCollector dc = new DataCollector();

                dataCollected.append("Begining Test...\n\n");
                dataCollected.append("Collecting System Information... \n\n");
                dataCollected.append(dc.getSystem());
                                ... lots more like this...

}

it doesn't actually update until everything in the mouseclick has run. 它实际上不会更新,直到mouseclick中的所有内容都已运行。

That's totally correct. 完全正确。 Your mouseClicked method is called on the GUI thread and this thread is the only thread which updates the GUI. 您的mouseClicked方法在GUI线程上被调用,并且该线程是唯一更新GUI的线程。 So your "update textarea content" action is executed after your mouseClicked method finishes. 因此mouseClicked方法完成后,将执行“更新文本区域内容”操作。 Therefore your methods which run on the GUI thread should run extremely quick so other methods which wants to run on the GUI thread can do so. 因此,在GUI线程上运行的方法应运行得非常快,以便其他要在GUI线程上运行的方法也可以这样做。

You can start a new thread which runs in parallel to your normal code which will update your JTextArea. 您可以启动一个与常规代码并行运行的新线程,这将更新您的JTextArea。 Read Lesson: Concurrency in Swing on how to work with threads in swing (and what the "Event Dispatch Thread" is). 阅读课程:Swing的并发性,了解如何在Swing中使用线程(以及“事件调度线程”是什么)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM