The following applet is produced from the code below:
The life cycle of a progress bar follows:
Declaration. (Globally so that you can change it.) | JProgressBar p; |
Construction. The numbers stand for the oriententation (0-horizontal, 1-vertical), the minimum value of the ProgressBar, the maximum value of the ProgressBar (In init) | p = new JProgressBar (0, 0, 100); |
Sets a value of the JProgressBar. (In init or actionPerformed) | p.setValue (25); |
Shows the percentage on the JProgressBar. (In init or actionPerformed) | p.setStringPainted (true); |
Adds the JProgressBar. (In init) | add (p); |
Sets the value of the JProgressBar, using a variable. (In init or in actionPerformed) | p.setValue (i); |
More details are found on:
http://java.sun.com/javase/6/docs/api/javax/swing/JProgressBar.html#JProgressBar()
http://java.sun.com/docs/books/tutorial/uiswing/components/progress.html
The compete code follows:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
public class Progress extends Applet implements ActionListener
{
JProgressBar p;
int i = 25;
public void init ()
{
p = new JProgressBar (0, 0, 100);
p.setValue (25);
p.setStringPainted (true);
add (p);
JButton done = new JButton ("Add");
done.setActionCommand ("Add");
done.addActionListener (this);
add (done);
}
public void actionPerformed (ActionEvent e)
{
i++;
p.setValue (i);
}
}