JTextArea附加问题

我做了一个备份程序,我希望所有的程序备份显示在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 CopyFolder { public static void copyFolder(File src, File dest) throws IOException { if (src.isDirectory()) { // if directory not exists, create it if (!dest.exists()) { dest.mkdir(); Panel.txtArea.append("Folder " + src.getName() + " was created\n"); } // 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.copyFile(src, dest); } catch (Exception e) { } } } } 

CopyFile类

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

感谢提前的帮助,并让我知道我可以给的任何援助。 我做了一个谷歌search,这似乎是一个大问题,但我只是不能想到如何解决它。 噢,请不要因为它不适用于你而使其失望,因为它非常让人烦恼。 再次感谢!

编辑 :这是我得到:

 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被更新,因为重绘请求没有到达重绘pipe理器,直到完成后。

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

为此,我build议您阅读Swing Trail中的并发性,这将为您提供一些有用的策略来解决您的特定问题。 特别是,你可能会使用SwingWorker

仔细看看doInBackground和处理方法

用例子更新

好的,这是一个非常简单的例子。 这基本上引导你C:\驱动器深入3个目录,并转储内容到提供的JTextArea

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

你只需要调用new BackgroundWorker(textField).execute()然后走开:D

用明确的例子更新

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