Java tutorial
import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.List; import javax.swing.JButton; import javax.swing.JCheckBox; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JProgressBar; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.SwingWorker; public class Main { public static void main(String[] args) { JFrame frame = new ProgressBarFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } } class ProgressBarFrame extends JFrame { int MAX = 1000; JButton startButton = new JButton("Start"); JProgressBar progressBar = new JProgressBar(0, MAX); JCheckBox checkBox = new JCheckBox("indeterminate"); JTextArea textArea = new JTextArea(); SimulatedActivity activity; public ProgressBarFrame() { setSize(300, 300); JPanel panel = new JPanel(); progressBar.setStringPainted(true); panel.add(startButton); panel.add(progressBar); checkBox.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { progressBar.setIndeterminate(checkBox.isSelected()); progressBar.setStringPainted(!progressBar.isIndeterminate()); } }); panel.add(checkBox); add(new JScrollPane(textArea), BorderLayout.CENTER); add(panel, BorderLayout.SOUTH); startButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { startButton.setEnabled(false); activity = new SimulatedActivity(MAX); activity.execute(); } }); } class SimulatedActivity extends SwingWorker<Void, Integer> { int current, target; public SimulatedActivity(int t) { current = 0; target = t; } protected Void doInBackground() throws Exception { try { while (current < target) { Thread.sleep(100); current++; publish(current); } } catch (Exception e) { } return null; } protected void process(List<Integer> chunks) { for (Integer chunk : chunks) { textArea.append(chunk + "\n"); progressBar.setValue(chunk); } } protected void done() { startButton.setEnabled(true); } } }