Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make my SwingWorker example work properly?

I've made my own SwingWorker example to get familiar with how it works.

What I'm wanting to do is the following: When the button is clicked I want a progress bar appear until the task is done I want to simply remove the progress bar and add a string to the dialog.

When the button is clicked, the progress bar comes up but never goes away. (never removes the progress bar after 10 seconds and never places the label up)

Here is an SSCCE:

package swingtesting;

import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JProgressBar;
import javax.swing.SwingWorker;

public class SwingTesting {

    /**
     * Creates a frame that will hold a simple button to make use of SwingWorker
     */
     public static void main(String[] args) {
         // TODO code application logic here
         JFrame frame = new JFrame();
         JButton button = new JButton();

         button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                new GuiWorker().execute();
            }
         });
         button.setText("Test Me");
         frame.getContentPane().add(button);
         frame.pack();
         frame.setVisible(true);
    }
}

class GuiWorker extends SwingWorker<Integer, Integer> {

    /*
     * This should just create a frame that will hold a progress bar until the
     * work is done. Once done, it should remove the progress bar from the dialog
     * and add a label saying the task complete.
     */

    private JFrame frame = new JFrame();
    private JDialog dialog = new JDialog(frame, "Swingworker test", true);
    private JProgressBar progressBar = new JProgressBar();


    public GuiWorker() {
        progressBar.setString("Waiting on time");
        progressBar.setStringPainted(true);
        progressBar.setIndeterminate(true);
        dialog.getContentPane().add(progressBar);
        dialog.pack();
        dialog.setVisible(true);
    }

    @Override
    protected Integer doInBackground() throws Exception {
        Thread.sleep(10000);
        return 0;
    }

    @Override
    protected void done() {
        JLabel label = new JLabel("Task Complete");
        dialog.getContentPane().remove(progressBar);
        dialog.getContentPane().add(label);
    }

}
like image 372
WilliamShatner Avatar asked Apr 19 '12 21:04

WilliamShatner


People also ask

How does SwingWorker work?

SwingWorker is designed for situations where you need to have a long running task run in a background thread and provide updates to the UI either when done, or while processing. Subclasses of SwingWorker must implement the doInBackground() method to perform the background computation.

How do I stop being a SwingWorker?

To cancel a running background task, invoke SwingWorker. cancel The task must cooperate with its own cancellation.

What happens when SwingWorker task is not finished?

If the SwingWorker object has not finished executing the doInBackground() method, the call to this method blocks until the result is ready. It is not suggested to call this method on the event dispatch thread, as it will block all events until it returns. cancels the task if it is still running.

What is SwingUtilities invokeLater () used for?

An invokeLater() method is a static method of the SwingUtilities class and it can be used to perform a task asynchronously in the AWT Event dispatcher thread.

What is SwingWorker in Java?

To do so, we use a SwingWorker, which allows creating a background thread to perform our task and communicate to our main application the progress. The javax.swing.SwingWorker class was added to the Java platform in Java SE 6.

What is the life cycle of SwingWorker?

The old SwingWorker was not part of the Java platform specification, and was not provided as part of the JDK. There are three threads involved in the life cycle of a SwingWorker: Current thread: The execute () method is called on this thread. It schedules SwingWorker for the execution on a worker thread and returns immediately.

What are the parameters of a SwingWorker?

When creating a SwingWorker<T, V>, we define two parameters on the constructor; T — the result type returned by this SwingWorker's doInBackground () and get () methods, and V the type used for carrying out intermediate results by this SwingWorker's publish () and process () methods.

What is the use of doinbackground in SwingWorker?

SwingWorker is designed for such tricky situations which provides communication on event dispatch thread. doInBackground (): This function contains our logic of the background task i.e. what we want our thread to do. This function runs on worker thread and is necessary to implement.


2 Answers

Here an updated version of your code which works

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JProgressBar;
import javax.swing.SwingWorker;

public class SwingTesting {

  public static void main(String[] args) {
    EventQueue.invokeLater( new Runnable() {
      @Override
      public void run() {
        JFrame frame = new JFrame();
        JButton button = new JButton();
        button.addActionListener(new ActionListener() {
          @Override
          public void actionPerformed(ActionEvent e) {
            new GuiWorker().execute();
          }
        });
        button.setText("Test Me");
        frame.getContentPane().add(button);
        frame.pack();
        frame.setVisible(true);
      }
    } );

  }
}

class GuiWorker extends SwingWorker<Integer, Integer> {

  /*
  * This should just create a frame that will hold a progress bar until the
  * work is done. Once done, it should remove the progress bar from the dialog
  * and add a label saying the task complete.
  */

  private JFrame frame = new JFrame();
  private JDialog dialog = new JDialog(frame, "Swingworker test", true);
  private JProgressBar progressBar = new JProgressBar();


  public GuiWorker() {
    progressBar.setString("Waiting on time");
    progressBar.setStringPainted(true);
    progressBar.setIndeterminate(true);
    dialog.getContentPane().add(progressBar);
    dialog.pack();
    dialog.setModal( false );
    dialog.setVisible(true);
  }

  @Override
  protected Integer doInBackground() throws Exception {
    System.out.println( "GuiWorker.doInBackground" );
    Thread.sleep(1000);
    return 0;
  }

  @Override
  protected void done() {
    System.out.println("done");
    JLabel label = new JLabel("Task Complete");
    dialog.getContentPane().remove(progressBar);
    dialog.getContentPane().add(label);
    dialog.getContentPane().validate();
  }

}

Key point is that setting a model dialog visible blocks until the dialog is disposed. So making it non-modal fixed it + the validate call on the content pane when you switch components. I also adjusted your main method to run on the EDT, and added some System.out calls. If you remove the setModal( false ) call you will see those statements are not printed until you close the dialog

like image 180
Robin Avatar answered Sep 29 '22 19:09

Robin


There's no need to make the dialog non-modal. Simply display the dialog after starting the SwingWorker. This can be done either from the calling class, the one executing the SwingWorker, by first calling execute, and then showing the dialog, or it can be done from the SwingWorker, but if from the latter, you'll have to make your own pseudo-execute method that calls super's execute, and then shows the dialog. Note that you can't override execute() itself since it's final.

For example...

import java.awt.CardLayout;
import java.awt.Window;
import java.awt.Dialog.ModalityType;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.concurrent.ExecutionException;

import javax.swing.*;

@SuppressWarnings("serial")
public class SwingTesting2 {

   private static void createAndShowGui() {
      final JFrame frame = new JFrame("SwingTesting2");
      final JDialog dialog = new JDialog(frame, "Dialog",
            ModalityType.APPLICATION_MODAL);
      final DialogPanel dialogPanel = new DialogPanel();
      dialog.getContentPane().add(dialogPanel.getMainPanel());
      dialog.pack();
      dialog.setLocationRelativeTo(frame);

      JButton button = new JButton(new AbstractAction("Test Me") {

         @Override
         public void actionPerformed(ActionEvent actEvt) {
            final GuiWorker2 guiWorker = new GuiWorker2();
            guiWorker.addPropertyChangeListener(new PropertyChangeListener() {

               @Override
               public void propertyChange(PropertyChangeEvent pcEvt) {
                  if (pcEvt.getPropertyName().equals("state")) {
                     if (pcEvt.getNewValue() == SwingWorker.StateValue.DONE) {
                        try {
                           dialogPanel.done(guiWorker.get());
                        } catch (InterruptedException e) {
                           e.printStackTrace();
                        } catch (ExecutionException e) {
                           e.printStackTrace();
                        }
                     }
                  } else if (pcEvt.getPropertyName().equals("progress")) {
                     dialogPanel.setProgress((Integer)pcEvt.getNewValue());
                  }
               }
            });

            guiWorker.execute();
            dialogPanel.start();
            dialog.setVisible(true);
         }
      });

      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(button);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

class GuiWorker2 extends SwingWorker<Integer, Integer> {
   private static final int MAX_COUNT = 20;
   private static final long SLEEP_TIME = 100;
   private int count = 0;

   @Override
   protected Integer doInBackground() throws Exception {
      while (count < MAX_COUNT) {
         Thread.sleep(SLEEP_TIME);
         count++;
         setProgress((100 * count) / MAX_COUNT);
      }
      return count;
   }
}

@SuppressWarnings("serial")
class DialogPanel {
   public static final String PROGRESS_BAR = "Progress Bar";
   public static final String DONE = "Done";
   private static final int TIMER_DELAY = 2000;
   private CardLayout cardLayout = new CardLayout();
   private JPanel mainPanel = new JPanel(cardLayout);
   private JLabel doneLabel = new JLabel("Done", JLabel.CENTER);
   private JProgressBar progressBar = new JProgressBar();

   public DialogPanel() {
      progressBar.setString("Waiting on time");
      progressBar.setStringPainted(true);
      progressBar.setIndeterminate(false);

      mainPanel.add(progressBar, PROGRESS_BAR);
      mainPanel.add(doneLabel, DONE);
   }

   public void setProgress(Integer newValue) {
      progressBar.setValue(newValue);
   }

   public void start() {
      cardLayout.show(mainPanel, PROGRESS_BAR);
      progressBar.setValue(0);
   }

   public void done(int countValue) {
      doneLabel.setText(DONE + ". Count: " + countValue);
      cardLayout.show(mainPanel, DONE);
      new Timer(TIMER_DELAY, new ActionListener() {

         @Override
         public void actionPerformed(ActionEvent e) {
            Window win = SwingUtilities.getWindowAncestor(mainPanel);
            win.dispose();
         }
      }){{setRepeats(false);}}.start();
   }

   public JPanel getMainPanel() {
      return mainPanel;
   }

}
like image 33
Hovercraft Full Of Eels Avatar answered Sep 29 '22 20:09

Hovercraft Full Of Eels