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 7656845
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T12:52:27+00:00 2026-05-31T12:52:27+00:00

I am having some trouble with my swingworker thread, and the issue is when

  • 0

I am having some trouble with my swingworker thread, and the issue is when i cancel. Essentially, I have a a start button, which when clicked runs my swingworker.

From here my swing worker does a number of small things but the main thing is that it completes some long running reflection calls.

I also have a cancel button – which as you probably guessed cancels my swingworker. The issue however is that I have a bug which I just cannot pinpoint.

After cancelling the application mid way through its first run, the swing worker cancels perfectly, however, if i then click start again and let it run all the way through when it completes – the thread seems to pick back up from where i cancelled on the first run :/ . I have no idea why this is and it apepears as if the cancelled thread is resuming and actually remembering state. I know this is happening because on my gui I have a JLabel that says “processing element 1 of 15…” and it jumps back to the cancelled point.

public class Test
{
    private SwingWorker<Object, Integer> swingworker = initiate(); 
    private void startListener()
    {
        start.addActionListener(new ActionListener ()
         {    
            public void actionPerformed(ActionEvent  e) 
            {             
                runIt();  
            }
        }); 
    }

    private void runIt()
    {
        try
        {
            if(swingworker .isDone())
                swingworker = getSwingWorker();

            swingworker.execute();
        }
        catch (HeadlessException e) { } 
        catch(Exception e){LogFileWriter.log(e);}
    }   


    private SwingWorker<Object, Integer> getSwingWorker()
    {
        return new SwingWorker<Object, Integer>() 
        {
            Object o;

             protected Object doInBackground() throws Exception 
             { 
                 //do some short calcs
                 doLongCalc();
                 return o;
             }

             protected void done()
             { 
                 if(!swingworker .isCancelled()) {} //chnage the gui values like enable / disable button } 
                 else{}//it was cancelled so stop progress bar and stuff

             }
        };   
    }

    private void cancelListener()
    {
        cancel.addActionListener(new ActionListener ()
         {    
            public void actionPerformed(ActionEvent e) 
            {

                if(swingworker.getState() == SwingWorker.StateValue.STARTED || swingworker.getState() == SwingWorker.StateValue.DONE)
                {
                    swingworker .cancel(true);
                }
            }
        }); 
    }

    private void doLongCalc()
    {
        //wrap the taks in checks for cancelled or not
        if(!swingworker.isCancelled())//do something
        if(!swingworker.isCancelled())//do something
        if(!swingworker.isCancelled())//do something
        if(!swingworker.isCancelled())//do something
    }
}
  • 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-05-31T12:52:28+00:00Added an answer on May 31, 2026 at 12:52 pm

    I had found out that I was not actually catching and dealing with an exception correctly. As such the programme flow was continuing where it should have been stopping.

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

Sidebar

Related Questions

I having some trouble figuring this problem out where I have one button which
Having some trouble passing values from one class to another. I basically have a
After having some trouble with setting up a thread to start my MIDI sequencer
Im having some trouble writing a getstring function, this is what I have so
I am having some trouble with the Google Maps API . I have an
I'm having some trouble getting log4net to work from ASP.NET 3.5. This is the
Having some trouble sending properly formatted HTML e-mail from a PHP script. I am
I'm having some trouble opening an existing blackberry eclipse project I have to work
I`m having some trouble in understanding how delegates in C# work. I have many
Having some trouble getting this query to work correctly. mysql_query(DELETE FROM `table` WHERE `id`

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.