我如何使用swingworker类将文本附加到textarea? [英] How I append text to textarea with using swingworker class?

查看:106
本文介绍了我如何使用swingworker类将文本附加到textarea?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在循环中使用swingworkerclass在java中追加文本。例如:

I want to append text in loop with swingworkerclass in java. for ex :

while(true)
{
     mytextarea.append("sometext");
     if(some_condition)
     {break;}
}

我想和swingworker一起工作,因为我想看看textarea上的每一个uptade。
对于这段代码,我只看到我的过程完成时的更新。

I want this with swingworker because I want to see every uptade on textarea. For this code I only see update when my proccess done.

我不想在另一种情况下使用swingworker样本。
请在这里给我一些代码。谢谢。

I dont want swingworker samples for another situations. Please give me some code here.Thanks.

推荐答案

SwingWorker不在这里。您的代码未在EDT中运行,因此您看不到更新。您可以使用SwingUtilities.invokeLater(...)在EDT中执行代码。但是不要在EDT中执行整个while循环,因为这会阻止它并且没有更新/事件(重绘,鼠标点击)。这是一个简单的代码示例:

SwingWorker is not right here. Your code is not running in the EDT so you doesn´t see updates. You can use SwingUtilities.invokeLater(...) to execute your code in the EDT. But do not execute the whole while-loop in the EDT because this will block it and no updates / events (repaints, Mouseclicks). Here is a simply code-example:

    while(true) {
        SwingUtilities.invokeLater(new Runnable{
            public void run() {
                textfield.setText(....);
            }
         });
         if(condition) break;
     }

有关更多信息,请查看 http://java.sun.com/products/jfc/tsc/articles/threads/threads1.html 或本书: http://filthyrichclients.org

For more Information look at http://java.sun.com/products/jfc/tsc/articles/threads/threads1.html or this book: http://filthyrichclients.org

这篇关于我如何使用swingworker类将文本附加到textarea?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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