Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8379203
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T16:06:52+00:00 2026-06-09T16:06:52+00:00

Im making a backup program, and I want everything that i have the program

  • 0

Im making a backup program, and I want everything that i have the program backing up displayed on a JTextArea. well, it works, but only after the program is finished with the backup. How do i fix this? The code i have running this is here:

backup method

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

Copy Folder class:

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 class

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

Thanks for the help in advance, and let me know of any assistance i can give. I did a google search on this, and it does seem to be a big problem, but i just cant think of how to fix it. Oh, and please dont downvote this just because it doesnt apply to you, its very aggravating. Thanks in advance again!

EDIT:
This is what i got:

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);
}
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T16:06:53+00:00Added an answer on June 9, 2026 at 4:06 pm

    The main problem you’re having is you’re trying to perform blocking actions in the Event Dispatching Thread. This will prevent the UI from been updated as repaint requests are not reaching the repaint manager until AFTER you’ve finished.

    To over come this, you’re going to need to off load the blocking work (ie the back up process) to a separate thread.

    For this I suggest you have a read through the Concurrency in Swing Trail which will provide you with some useful strategies to solve your particular problem. In particular, you’ll probably benifit from using a SwingWorker

    Take a close look at doInBackground and the process methods

    UPDATED with Example

    Okay, so this is a REALLY simple example. This basically walks you C:\ drive to 3 directories deep and dumps the content to the supplied 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);
    
                }
    
            }
    
        }
    
    }
    

    You would simply call new BackgroundWorker(textField).execute() and walk away 😀

    UPDATED with explicit example

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

    Now to run…

    new BackgroundWorker(textArea, sourceDir, destDir).execute();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have 2 questions. So i am making a python program that will backup
I'm making a program in java that monitors and backup a directory. From time
I am making a program in C++ for Windows XP that requires sound to
I'm making a simple call to restore a mysql backup that takes about 45
Working on a backup laptop that doesn't have JD decompiler installed. Went to the
Since I'm making a full backup of my entire debian system, I was thinking
Making a new site but something is happening to it in IE8. The social
Making a word document of our network set-up. We have about 7 servers and
Making a new site but something is happening to it in IE. I've purchased
Making an adobe flex ui in which data that is calculated must use proprietary

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.