为什么我的JTextArea不更新? [英] Why is my JTextArea not updating?

查看:107
本文介绍了为什么我的JTextArea不更新?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有如下代码:

class SimplifiedClass extends JApplet {

    private JTextArea outputText;
    // Lots of methods
    public void DoEverything() {
        String output = "";
        for(int i = 0; i <= 10; i++) {
            output += TaskObject.someLongTask(i);
            outputText.setText(output);
        }
    }
}

当调用setText时,在循环的每次迭代之后,文本区域似乎仅在任务的所有运行完成时才更新文本。为什么会发生这种情况,如何解决?

However, instead of updating the text area after each iteration of the loop when setText is called, it appears to only update the text when all the runs of the task are done. Why does this happen, and how can I resolve it?

推荐答案

您可能使用了Swing线程,代码在更新UI之前执行。尝试对该循环使用单独的线程。

You're probably using the Swing thread which is waiting for your code to execute before it can update the UI. Try using a separate thread for that loop.

public void DoEverything() {
  SwingUtilities.invokeLater(new Runnable() {
    public void run() {
      String output = "";
      for(int i = 0; i <= 10; i++) {
        output += TaskObject.someLongTask(i);
        outputText.setText(output);
      }
    }
  });
}

这篇关于为什么我的JTextArea不更新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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