java - 需要在JTextArea中实时更新

标签 java multithreading swing jtextarea

我试图让这个名为 textAreaJTextArea 在复制这些照片时进行更新,但我似乎无法让它正常工作。我正在使用这段代码:

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();
                }
            }
        }
    });
}

结果相同。有什么建议吗?

另外,有什么方法可以让文本出现在文本区域的顶部吗?

最佳答案

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

您应该做的是在工作线程上执行繁重的工作,并且仅在 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();
}

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

关于java - 需要在JTextArea中实时更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10474963/

相关文章:

Java - 将转义字符串转换为字符

java - Apache 共享数学 : where are the values for SummaryStatistics stored?

python - 如何实现暂停(和更多)功能?

android - 没有IPC的Android进程默认有Binder线程池吗?

Java无法将JPanel内的JTextField作为JScrollPane中的视口(viewport)来关注

java - 如何使用 Ant 为具有外部 jar 依赖项的 java 项目构建可分发的 jar

java - 什么是非法反射访问?

ruby - Ruby检查线程是否为 “free”

java - 在JTable中显示来自mysql的数据

java - 根据深度级别更改 JTree 节点图标