实时输出到jTextArea [英] Output to jTextArea in realtime

查看:144
本文介绍了实时输出到jTextArea的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些代码需要花费几分钟来处理,它必须连接到网络中的长数组中的每个字符串,每个字符串都是一个网址。我想这样做,以便每次连接时,都应该刷新jtextarea,这样用户就不会凝视一个看起来冻结20分钟的空白页面。或者需要多长时间。这里有一个我尝试过但没有工作的例子:

I have some code which takes a few minutes to process, it has to connect to the web for each string in a long array, each string is a url. I want to make it so that everytime it connects, it should refresh the jtextarea so that the user is not staring into a blank page that looks frozen for 20 min. or however long it takes. here is an example of something i tried and didnt work:

try {
            ArrayList<String> myLinks = LinkParser.getmyLinksArray(jTextArea1.getText());
            for (String s : myLinks) {
                jTextArea2.append(LinkChecker.checkFileStatus(s) + "\n");
            }
        } catch (IOException ex) {
            JOptionPane.showMessageDialog(jTextArea1, "Parsing Error", "Parsing Error", JOptionPane.ERROR_MESSAGE);
            Logger.getLogger(MYView.class.getName()).log(Level.SEVERE, null, ex);
        }


推荐答案

问题在于你需要异步执行计算。您应该创建一个执行计算的后台线程,然后使用 SwingUtilities.invokeLater 更新JTextArea。

The problem is that you need to perform the computation asynchronously. You should create a background thread that performs the computation, and then use SwingUtilities.invokeLater to update the JTextArea.


final ArrayList<String> myLinks = //...
(new Thread()
{
    public void run(){
        for (String s : myLinks) {
            try{
               final String result = LinkChecker.checkFileStatus(s) + "\n";
               SwingUtilities.invokeLater(new Runnable(){ 
                    public void run(){    
                      jtextArea2.append(result);
                    }
                });
             }catch(IOException error){
                // handle error
             }
        }
    }
}).start();

编辑

有人指出JTextArea的追加功能实际上是线程安全(与大多数Swing功能不同)。因此,对于这种特殊情况,没有必要通过invokeLater更新它。但是,您仍然应该在后台线程中进行处理以允许GUI更新,因此代码为:

Edit
It has been pointed out that JTextArea's append function actually is thread safe (unlike most Swing functions). Therefore, for this particular, case it is not necessary to update it via invokeLater. However, you should still do you processing in a background thread so as to allow the GUI to update, so the code is:


final ArrayList<String> myLinks = //...
(new Thread()
{
    public void run(){
        for (String s : myLinks) {
            try{
               jtextArea2.append(LinkChecker.checkFileStatus(s) + "\n");
             }catch(IOException error){
                // handle error
             }
        }
    }
}).start();

但是,对于修改Swing对象的几乎任何其他操作,您将需要使用invokeLater(以确保修改发生在GUI线程),因为几乎所有的Swing函数都不是线程安全的。

However, for pretty much any other operation that modifies a Swing object, you will need to use invokeLater (to ensure the modification occurs in the GUI thread), since almost all the Swing functions aren't thread safe.

这篇关于实时输出到jTextArea的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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