package de.thomas.thread;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
public class ThreadApp2 extends JFrame implements ActionListener, Runnable {
private static final long serialVersionUID = 1L;
private JButton btnStartProgress;
private JButton btnPauseProgress;
private JButton btnStopProgress;
private JProgressBar barProgressRed;
private JProgressBar barProgressOrange;
private JProgressBar barProgressGreen;
private JLabel lblProgress;
private int iCount = 0;
private final int maxCount = 100;
private final String AC_START_PROGRESS = "startProgress";
private final String AC_PAUSE_PROGRESS = "pauseProgress";
private final String AC_STOP_PROGRESS = "stopProgress";
Thread t;
private boolean suspend = false;
private boolean stop = false;
public ThreadApp2()
{
initComponents ();
setLocationRelativeTo( null );
}
public static void main (String[] args)
{
java.awt.EventQueue.invokeLater(new Runnable()
{
public void run()
{
new ThreadApp2().setVisible(true);
}
});
}
private void initComponents()
{
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
btnStartProgress = new JButton("Start");
btnStartProgress.addActionListener (this);
btnStartProgress.setActionCommand (AC_START_PROGRESS);
btnPauseProgress = new JButton("Pause");
btnPauseProgress.addActionListener (this);
btnPauseProgress.setActionCommand (AC_PAUSE_PROGRESS);
btnPauseProgress.setEnabled (false);
btnStopProgress = new JButton("Stop");
btnStopProgress.addActionListener (this);
btnStopProgress.setActionCommand (AC_STOP_PROGRESS);
btnStopProgress.setEnabled (false);
barProgressRed = new JProgressBar();
barProgressRed.setMinimum (0);
barProgressRed.setMaximum (24);
barProgressRed.setValue (0);
barProgressRed.setBorderPainted (false);
barProgressRed.setForeground (Color.RED);
barProgressRed.setPreferredSize (new Dimension (100, 15));
barProgressOrange = new JProgressBar();
barProgressOrange.setMinimum (25);
barProgressOrange.setMaximum (74);
barProgressOrange.setValue (25);
barProgressOrange.setBorderPainted (false);
barProgressOrange.setForeground (Color.ORANGE);
barProgressOrange.setPreferredSize (new Dimension (200, 15));
barProgressGreen = new JProgressBar();
barProgressGreen.setMinimum (75);
barProgressGreen.setMaximum (100);
barProgressGreen.setValue (75);
barProgressGreen.setBorderPainted (false);
barProgressGreen.setForeground (Color.GREEN);
barProgressGreen.setPreferredSize (new Dimension (100, 15));
lblProgress = new JLabel(Integer.toString (barProgressRed.getValue ()) + "%");
lblProgress.setPreferredSize (new Dimension (32, 16));
this.getContentPane().setLayout (new FlowLayout());
this.getContentPane().add (btnStartProgress);
this.getContentPane().add (btnPauseProgress);
this.getContentPane().add (btnStopProgress);
JPanel panProgress = new JPanel (new GridBagLayout ());
panProgress.add (barProgressRed, new GridBagConstraints (1,0,1,1,0.0,0.0,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,new Insets (0,0,0,0), 0,0));
panProgress.add (barProgressOrange, new GridBagConstraints (2,0,1,1,0.0,0.0,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,new Insets (0,0,0,0), 0,0));
panProgress.add (barProgressGreen, new GridBagConstraints (3,0,1,1,0.0,0.0,GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,new Insets (0,0,0,0), 0,0));
this.getContentPane().add (panProgress);
this.getContentPane().add (lblProgress);
this.pack ();
}
public void actionPerformed (ActionEvent ae)
{
if ( ae.getActionCommand () == AC_START_PROGRESS) {
iCount = 0;
btnStartProgress.setEnabled (false);
btnPauseProgress.setEnabled (true);
btnStopProgress.setEnabled (true);
t = new Thread(this);
t.start ();
}
else if ( ae.getActionCommand () == AC_PAUSE_PROGRESS) {
if (suspend) {
//System.out.println ("isNotPause");
resumeThread ();
}
else {
//System.out.println ("isPause");
suspendThread ();
}
}
else if ( ae.getActionCommand () == AC_STOP_PROGRESS) {
stopThread ();
btnPauseProgress.setEnabled (false);
btnStopProgress.setEnabled (false);
}
}
public void run () {
while (!t.isInterrupted () && iCount <= maxCount) {
try {
waitForResumeOrStop ();
barProgressRed.setValue (iCount);
barProgressOrange.setValue (iCount);
barProgressGreen.setValue (iCount);
if (iCount >= 75) {
lblProgress.setText (Integer.toString (barProgressGreen.getValue ()) + "%");
}
else if (iCount >= 25) {
lblProgress.setText (Integer.toString (barProgressOrange.getValue ()) + "%");
}
else {
lblProgress.setText (Integer.toString (barProgressRed.getValue ()) + "%");
}
iCount ++;
Thread.sleep(100);
}
catch (InterruptedException e) {
t.interrupt ();
}
}
btnStartProgress.setEnabled (true);
btnPauseProgress.setEnabled (false);
btnStopProgress.setEnabled (false);
stop = false;
suspend = false;
}
public void suspendThread () {
suspend = true;
}
public void resumeThread () {
suspend = false;
synchronized (this) {
this.notifyAll ();
}
}
public void stopThread () {
stop = true;
t.interrupt ();
}
public final void waitForResumeOrStop () throws InterruptedException {
synchronized (this) {
while (suspend) {
this.wait ();
if (stop)
throw new InterruptedException ();
}
if (stop)
throw new InterruptedException ();
}
}
}