getting the cancel event of Java ProgressMonitor

后端 未结 2 1575
醉酒成梦
醉酒成梦 2020-12-11 19:33

I have a ProgressMonitor pm and a SwingWorker sw. I want to cancel the SwingWorker when I press the cancel-button on pm.

2条回答
  •  误落风尘
    2020-12-11 20:28

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

提交回复
热议问题