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

  • SEARCH
  • Home
  • 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 8449949
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T10:50:25+00:00 2026-06-10T10:50:25+00:00

Im making a program that is designed to be a backup. i have SwingWorker

  • 0

Im making a program that is designed to be a backup. i have SwingWorker doing the backup and posting what it’s doing to a JTextArea. I want a button to cancel the worker, (there are a unknown amount of them initiallized at one time) so this is what i have for the calling of the swing workers, and the cancel method:

package main;

import java.io.File;
import java.util.ArrayList;

import javax.swing.SwingWorker;

public class test2 {

SwingWorker bw;
static ArrayList bgWorker = new ArrayList();
ArrayList al = new ArrayList(); // this is the list of files selected
static boolean bwInitiallized = false;

public void startBackup() throws Exception {
    Panel.txtArea.append("Starting Backup...\n");

    for (int i = 0; i < al.size(); i++) {
        /**
         * THIS IS WHERE I NEED TO CREATE THE FOLDER THAT EACH BACKUP FILE
         * WILL GO INTO EX: SC2 GOES INTO A FOLDER CALLED SC2 AND RIOT GOES
         * TO RIOT, ALL WITHIN THE DIRECTORY CHOSEN
         */
        File file = new File((String) al.get(i));
        File directory = new File(dir); // dir is gotten by a JFileChooser.

        bw = new BackgroundWorker(Panel.txtArea, file, directory);
        bgWorker.add(bw);
        bwInitiallized = true;
        bw.execute();

        /**
         * follows to the bottom of the txtarea
         */
        int x;
        Panel.txtArea.selectAll();
        x = Panel.txtArea.getSelectionEnd();
        Panel.txtArea.select(1, x);
    }
}

public static void cancel() {
    BackgroundWorker bg;

    if (bwInitiallized) {
        bwInitiallized = false;
        Panel.txtArea.append("Cancelling...\n");
        for (int i = 0; i < bgWorker.size(); i++) {
            bg = (BackgroundWorker) bgWorker.get(i);
            bg.cancel(true);
        }
        Panel.txtArea.append("Canceled backUp!\n");
    } else {
        Panel.txtArea.append("Cannot Cancel! Not Initiallized!\n");
    }
}
}

Well, for reasons that i cannot figure out, this does not cancel any of them (as far as i’m aware). the only thing that i can think of is when i do

bg = (BackgroundWorker) bgWorker.get(i);
bg.cancel(true);

it doesnt do what i think it does, but idk. what am i doing wrong? thanks in advance!

  • 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-10T10:50:27+00:00Added an answer on June 10, 2026 at 10:50 am

    I’d create an instance of class FileWorker extends SwingWorker<File, File> for each file and a single instance of class Supervisor extends SwingWorker<Queue<File>, File> to manage them, as suggested in this example. You could allow the user to cancel an individual FileWorker or let the Supervisor cancel them all. An example using cancel() is shown here.

    In any case, use the appropriate SwingWorker type parameters for safety and critically examine your design as suggested in comments by @Hovercraft.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been making a program that creates multiple CSV's from another source CSV
I am making a program that solves an equation. I have a variable that
I am making a program that takes input from files that I have called
I'm making a program that will have a widget that has to be fixed
I am making a program that generates a Javascript file. I have never worked
I am making a program that automates the seperation of a csv file. We
I am making a program that graphs a line based on data inputted by
I am currently making a program that will send an email if the date
I noticed while making a program that a lot of my int type variables
I'm making a program that communicate with certain patient monitor using C sockets. I'm

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.