import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import org.jdesktop.swingworker.*;
import java.util.concurrent.*;

public class SwingWorkerExample extends JFrame implements ActionListener {
  private JButton start_button;
  private JButton get_button;
  private JProgressBar bar;
  private JLabel label;

  private MySwingWorker worker;

  public SwingWorkerExample() {
    this.setLayout(new FlowLayout());
    start_button = new JButton("Load");
    start_button.addActionListener(this);
    bar = new JProgressBar(0,100);
    get_button = new JButton("Get");
    get_button.addActionListener(this);
    get_button.setEnabled(false);
    label = new JLabel("            ");
    this.add(start_button);
    this.add(bar);
    this.add(get_button);
    this.add(label);
  }

  private static void createAndShowGUI() {
    SwingWorkerExample swe = new SwingWorkerExample();
    swe.pack();
    swe.setVisible(true);
  }

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

  public void actionPerformed(ActionEvent event) {
    Component c = (Component)event.getSource();
    if (c == start_button) {
      start_button.setEnabled(false);
      get_button.setEnabled(false);
      worker = new MySwingWorker(start_button, bar, get_button);
      worker.execute();
    } 
    if (c == get_button) {
      try { label.setText(worker.get()); }
      catch (InterruptedException e) {}
      catch (ExecutionException e) {}
      get_button.setEnabled(false);
    }
  }
}
