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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T08:20:47+00:00 2026-06-10T08:20:47+00:00

I have a ProgressMonitor pm and a SwingWorker sw . I want to cancel

  • 0

I have a ProgressMonitor pm and a SwingWorker sw. I want to cancel the SwingWorker when I press the cancel-button on pm. I guess this shouldnt be too hard, and I read some tutorials about SwingWorker and ProgressMonitor, but I can’t get this to work.

final ProgressMonitor pm = new ProgressMonitor(frame, "checking", "...", 0, 100);
final SwingWorker sw = new SwingWorker()
{
    protected Object doInBackground() throws Exception 
    {
        doSomethingAndUpdateProgress();
    }
};

sw.addPropertyChangeListener(new PropertyChangeListener()
{
    public void propertyChange(PropertyChangeEvent evt)
    {
        if(evt.getPropertyName().equals("progress"))
        {
            updateProgress();
        }
        if(pm.isCanceled())
        {
            cancelAction();
        }
        if(pm.isDone())
        {
            doneAction();
        }
    }
});

sw.execute();

Progress updating is working just fine, but pm.isCanceled() is never true. I suppose I need a propertyChangeListener for the ProgressMonitor, but I dont know how I would add one there.

  • 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-10T08:20:49+00:00Added an answer on June 10, 2026 at 8:20 am

    During the execution of your long running task, you want to periodically check if the ProgressMonitor was canceled. It’s your job to check that at points where it makes sense to cancel the task – otherwise who knows what resources you could leave hanging.

    So basically, you want to change your doSomethingAndUpdateProgress() method so that it also checks if the ProgressMonitor was canceled.

    Here’s a demo that shows how this works:

    import java.awt.*;
    import javax.swing.*;
    
    public class TempProject extends Box{
    
        public TempProject(){
            super(BoxLayout.Y_AXIS);
            final ProgressMonitor pm = new ProgressMonitor(this, "checking", "...", 0, 100);
            final SwingWorker sw = new SwingWorker<Integer, Integer>()
            {
                protected Integer doInBackground() throws Exception 
                {
                    int i = 0;
                    //While still doing work and progress  monitor wasn't canceled
                     while (i++ < 100 && !pm.isCanceled()) {
                         System.out.println(i);
                         publish(i);
                         Thread.sleep(100);
                     }
                     return null;
                }
    
    
                 @Override
                 protected void process(java.util.List<Integer> chunks) {
                     for (int number : chunks) {
                         pm.setProgress(number);
                     }
                 }
    
            };
    
            sw.execute();
        }
    
    
        public static void main(String args[])
        {
            EventQueue.invokeLater(new Runnable()
            {
                public void run()
                {
                    JFrame frame = new JFrame();
                    frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
                    frame.setContentPane(new TempProject());    
                    frame.pack();
                    frame.setVisible(true);
                }
            });
        }   
    
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Have some dates in my local Oracle 11g database that are in this format:
I'm trying to create ProgressMonitor with changing totalWork. Lets assume that I have 2
have written this little class, which generates a UUID every time an object of
have a problem. At first look at this HTML <div id=map style=background-image: url(map.png); width:
Have converted devise new session from erb to Haml but doens't work, this is
have anyone can tell me what syntax error on this actionscript (actionscript3.0)? var rotY:
Have finally got a responsive site working (of a fashion). What I want to
have a php code like this,going to convert it in to C#. function isValid($n){
Have data that has this kind of structure. Will be in ascending order by
I'd like to make use of ProgressMonitor 's functionality but I don't want to

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.