Java JTextArea附加问题

Java JTextArea附加问题,java,multithreading,swing,jtextarea,swingworker,Java,Multithreading,Swing,Jtextarea,Swingworker,我正在制作一个备份程序,我希望所有我备份的程序都显示在JTextArea上。好吧,这是可行的,但只有在程序完成备份之后。我该如何解决这个问题?我在这里运行的代码如下: 备份方法 public void startBackup() throws Exception { // txtarea is the JTextArea Panel.txtArea.append("Starting Backup...\n"); for (int i = 0; i < al.siz

我正在制作一个备份程序,我希望所有我备份的程序都显示在JTextArea上。好吧,这是可行的,但只有在程序完成备份之后。我该如何解决这个问题?我在这里运行的代码如下:

备份方法

public void startBackup() throws Exception {
    // txtarea is the JTextArea
    Panel.txtArea.append("Starting Backup...\n");

    for (int i = 0; i < al.size(); i++) {
        //al is an ArrayList that holds all of the backup assignments selected
        // from the JFileChooser

        File file = new File((String) al.get(i));
        File directory = new File(dir);

        CopyFolder.copyFolder(file, directory);
            }
     }
复制文件类

public class CopyFile {

public static void copyFile(File src, File dest) throws Exception {
    // if file, then copy it
    // Use bytes stream to support all file types
    InputStream in = new FileInputStream(src);
    OutputStream out = new FileOutputStream(dest);

    byte[] buffer = new byte[1024];

    int length;
    // copy the file content in bytes
    while ((length = in.read(buffer)) > 0) {
        out.write(buffer, 0, length);
    }

    in.close();
    out.close();
    // System.out.println("File copied from " + src + " to " + dest);
    Panel.txtArea.append("File copied " + src.getName() + "\n");
}
    }
提前感谢您的帮助,并让我知道我能提供的任何帮助。我在谷歌上搜索了一下,这似乎是个大问题,但我想不出怎么解决。哦,请不要因为这不适用于你就否决这一点,这很让人恼火。再次提前感谢

编辑: 这就是我得到的:

public class test extends SwingWorker<Void, String> {
String txt;
JTextArea txtArea = null;

public test(JTextArea txtArea, String str) {
    txt = str;
    this.txtArea = txtArea;
}

protected Void doInBackground() throws Exception {

    return null;
}

protected void process(String str) {
    txtArea.append(str);
}

protected void getString() {
    publish(txt);
}
    }

您遇到的主要问题是您试图在中执行阻塞操作。这将防止UI被更新,因为重新绘制请求直到完成后才到达重新绘制管理器

为了解决这个问题,您需要将阻塞工作(即备份进程)卸载到单独的线程

为此,我建议您通读一下,它将为您提供一些有用的策略来解决您的特定问题。特别是,使用

仔细看一看这些方法

更新了示例

好的,这是一个非常简单的例子。这基本上会将您的C:\drive引导到3个目录深处,并将内容转储到提供的JTextArea

现在开始运行

new BackgroundWorker(textArea, sourceDir, destDir).execute();

不,因为你只是让事件调度线程进入睡眠状态。您需要执行EDT的备份工作,并将更新请求重新同步到EDT SwingUtilities中。invokeLater对此很有用,但坦率地说,您的问题与SwingWorker APIs有关。因此,建议的内容有什么问题?从本质上说,它做你刚刚做的事情asked@MadProgrammer是正确的;你的编辑是错误的。遵循API中的示例或。我试图使用SwingWorker,但我无法让它正常工作!选中“新建编辑”以查看我是如何制作和编辑的initiallized@wbAnon阅读我的例子。您不需要线程,SwingWorker将为您创建线程。除EDT外,不要修改任何线程中的Swing组件。您是否使用doBackground方法并在屏幕上发布您想要更新的内容。如果您试图从SwingWorker的doInBackground方法中进行Swing调用,请使用process方法对UIG进行更新,这与您想要执行的操作完全相反。请阅读教程以了解如何使用此工具。哦,很抱歉,我在重新编辑时忘记删除它。很抱歉,睡个好觉可能会有帮助。现在就去睡觉。所以如果我不回答。
public class BackgroundWorker extends SwingWorker<Object, File> {

    private JTextArea textArea;

    public BackgroundWorker(JTextArea textArea) {

        this.textArea = textArea;

    }

    @Override
    protected Object doInBackground() throws Exception {

        list(new File("C:\\"), 0);

        return null;

    }

    @Override
    protected void process(List<File> chunks) {

        for (File file : chunks) {

            textArea.append(file.getPath() + "\n");

        }

        textArea.setCaretPosition(textArea.getText().length() - 1);

    }

    protected void list(File path, int level) {

        if (level < 4) {

            System.out.println(level + " - Listing " + path);

            File[] files = path.listFiles(new FileFilter() {

                @Override
                public boolean accept(File pathname) {

                    return pathname.isFile();

                }
            });

            publish(path);
            for (File file : files) {

                System.out.println(file);
                publish(file);

            }

            files = path.listFiles(new FileFilter() {

                @Override
                public boolean accept(File pathname) {

                    return pathname.isDirectory() && !pathname.isHidden();

                }
            });

            for (File folder : files) {

                list(folder, level + 1);

            }

        }

    }

}
public class BackgroundWorker extends SwingWorker<Object, String> {

    private JTextArea textArea;
    private File sourceDir;
    private File destDir;

    public BackgroundWorker(JTextArea textArea, File sourceDir, File destDir) {

        this.textArea = textArea;
        this.sourceDir = sourceDir;
        this.destDir = destDirl

    }

    @Override
    protected Object doInBackground() throws Exception {

        if (sourceDir.isDirectory()) {

            // if directory not exists, create it
            if (!destDir.exists()) {
                destDir.mkdir();
                publish("Folder " + sourceDir.getName() + " was created");
            }

            // list all the directory contents
            String files[] = sourceDir.list();

            for (String file : files) {
                // construct the src and dest file structure
                File srcFile = new File(sourceDir, file);
                File destFile = new File(destDir, file);
                // recursive copy
                copyFolder(srcFile, destFile);
            }

        } else {
            try {
                copyFile(sourceDir, destDir);
            } catch (Exception e) {
            }
        }

        return null;

    }

    public void copyFolder(File src, File dest) throws IOException {

        if (src.isDirectory()) {

            // if directory not exists, create it
            if (!dest.exists()) {

                publish("Folder " + src.getName() + " was created");
            }

            // list all the directory contents
            String files[] = src.list();

            for (String file : files) {
                // construct the src and dest file structure
                File srcFile = new File(src, file);
                File destFile = new File(dest, file);
                // recursive copy
                copyFolder(srcFile, destFile);
            }

        } else {
            try {
                copyFile(src, dest);
            } catch (Exception e) {
            }
        }
    }

    public void copyFile(File src, File dest) throws Exception {
        // if file, then copy it
        // Use bytes stream to support all file types
        InputStream in = new FileInputStream(src);
        OutputStream out = new FileOutputStream(dest);

        byte[] buffer = new byte[1024];

        int length;
        // copy the file content in bytes
        while ((length = in.read(buffer)) > 0) {
            out.write(buffer, 0, length);
        }

        in.close();
        out.close();
        publish("File copied " + src.getName());

    }

    @Override
    protected void process(List<String> chunks) {

        for (String msg : chunks) {

            textArea.append(msg + "\n");

        }

        textArea.setCaretPosition(textArea.getText().length() - 1);

    }
}
new BackgroundWorker(textArea, sourceDir, destDir).execute();