【问题标题】:Need Realtime updating in JTextArea需要在 JTextArea 中实时更新
【发布时间】:2012-05-06 22:53:43
【问题描述】:

我正在尝试让这个名为textArea 的JTextArea 在复制这些照片时进行更新,但我似乎无法让它工作。我正在使用此代码:

String name = "";
    int numberOfPicturesCopied = 0;
    while (pictures.isEmpty() == f) {
        try {
            File tmp = pictures.firstElement();
            name = tmp.getName();
            String filename = destination + Meta.date(tmp) + tmp.getName();
            Path source = tmp.toPath();
            File destFile = new File(filename);
            Path destination = destFile.toPath();
            Files.copy(source, destination,
                    StandardCopyOption.COPY_ATTRIBUTES);
            textArea.append("Copied " + name + "\n");
            pictures.removeElementAt(0);
            numberOfPicturesCopied++;
        } catch (FileAlreadyExistsException faee) {
            textArea.append("Skipped " + name
                    + ": Picture Already In Computer\n");
        } catch (NoSuchFileException ncfe) {
            File tmp = pictures.firstElement();
            String filename = destination + Meta.date(tmp);
            File newDir = new File(filename);
            newDir.mkdir();
        } catch (IOException ee) {
            // TODO Auto-generated catch block
            ee.printStackTrace();
        }
    }

然后我把它改成这样:

public void copyPictures(){
    SwingUtilities.invokeLater(new Thread(){
        public void run(){
            String name = "";
            while(pictures.isEmpty() == f){
                try {
                    File tmp = pictures.firstElement();
                    name = tmp.getName();
                    String filename = destination + Meta.date(tmp) + tmp.getName();
                    Path source = tmp.toPath();
                    File destFile = new File(filename);
                    Path destination = destFile.toPath();
                    Files.copy(source, destination, StandardCopyOption.COPY_ATTRIBUTES);
                    textArea.append("Copied " + name + "\n");
                    pictures.removeElementAt(0);
                    numberOfPicturesCopied++;
                } catch(FileAlreadyExistsException faee){
                    textArea.append("Skipped " + name +": Picture Already In Computer\n");
                } catch (NoSuchFileException ncfe){
                    File tmp = pictures.firstElement();
                    String filename = destination + Meta.date(tmp);
                    File newDir = new File(filename);
                    newDir.mkdir();
                } catch (IOException ee) {
                    // TODO Auto-generated catch block
                    ee.printStackTrace();
                }
            }
        }
    });
}

同样的结果。有什么建议吗?

另外,有什么办法让文本进入文本区域的顶部?

【问题讨论】:

  • 1) 为了尽快获得更好的帮助,请发帖 SSCCE 表示您的最佳尝试。您尝试的其他事情可以在(例如)copyPictures2() 方法中指示。 2) 看来您正在阻止 EDT。使用SwingWorker。
  • SwingUtilities.invokeLater(new Thread()) 完全错误。您需要将要在后台线程上执行的文件 I/O 工作与应在 EDT 上完成的 Swing 组件的更新完全分开。

标签: java multithreading swing jtextarea


【解决方案1】:

已经回答了如何在开头插入文本。您问题的另一部分与往常一样......您正在事件调度线程上执行繁重的工作,该线程不再能够执行重绘。

您应该做的是在工作线程上执行繁重的工作,并且只在 EDT 上更新 UI。例如,您可以使用专为此设计的SwingWorker。或者更简单,使用您当前的代码并进行一些简单的修改

public void copyPictures(){
    new Thread(){
        public void run(){
            while(pictures.isEmpty() == f){
                try {
                    File tmp = pictures.firstElement();
                    final String name = tmp.getName();
                    String filename = destination + Meta.date(tmp) + tmp.getName();
                    Path source = tmp.toPath();
                    File destFile = new File(filename);
                    Path destination = destFile.toPath();
                    Files.copy(source, destination, StandardCopyOption.COPY_ATTRIBUTES);

                    SwingUtilities.invokeLater( 
                      new Runnable(){
                       public void run(){
                        textArea.append("Copied " + name + "\n");
                       }
                      }
                    );                    

                    pictures.removeElementAt(0);
                    numberOfPicturesCopied++;
                } catch(FileAlreadyExistsException faee){
                    textArea.append("Skipped " + name +": Picture Already In Computer\n");
                } catch (NoSuchFileException ncfe){
                    File tmp = pictures.firstElement();
                    String filename = destination + Meta.date(tmp);
                    File newDir = new File(filename);
                    newDir.mkdir();
                } catch (IOException ee) {
                    // TODO Auto-generated catch block
                    ee.printStackTrace();
                }
            }
        }
    }.run();
}

查看如何在单独的Thread 上完成工作,但 UI 已在 EDT 上更新。更多信息可以在 Swing Concurrency tutorial 或 SO 上找到(您的搜索关键字是 SwingWorker,这将导致大量示例,因为这是一个日常问题)

【讨论】:

    【解决方案2】:

    不确定你在问什么,标题似乎是在说文本没有更新,但你的问题似乎表明它没有被插入到你想要的位置......

    如果是后者,请改用插入方法

        textArea.insert("Copied " + name + "\n",0);
    

    把它放在文本区域的顶部。

    【讨论】:

      猜你喜欢
      • 2015-11-14
      • 1970-01-01
      • 2018-06-21
      • 1970-01-01
      • 2014-01-23
      • 2015-12-20
      • 1970-01-01
      • 1970-01-01
      • 2014-02-16
      相关资源
      最近更新 更多