The JProgressBar

The JProgressBar allows you to show an indication of progress in a graphical form to the user. It is relatively simple to use and implement, as can be seen in the following example.

Code Listing 13: Using the JProgressBar

start example
import java.awt.*; import java.awt.event.*; import javax.swing.*;     public class JProgressBarExample extends JFrame implements     ActionListener {     public static void main(String[] argv)     {         JProgressBarExample mainApp = new JProgressBarExample();     }          public JProgressBarExample()     {         super("JProgressBar Example");         setBounds(0, 0, 500, 100);         getContentPane().setLayout(null);         setDefaultCloseOperation(EXIT_ON_CLOSE);                         // Create the progress bar...         progressbar = new JProgressBar(JProgressBar.HORIZONTAL, 0,             100);         progressbar.setBounds(10, 20, 250, 25);                  // Create a button to add progress to the bar...         addProgressButton = new JButton("Add 1 to Progress");         addProgressButton.setBounds(270, 20, 200, 25);         addProgressButton.addActionListener(this);                  // Create a label to show a text representation of the         // progress...         label = new JLabel("0% Completed");         label.setLocation(350, 50);         label.setSize(label.getPreferredSize());                  // Add the labels to the content pane         getContentPane().add(progressbar);         getContentPane().add(addProgressButton);         getContentPane().add(label);                  setVisible(true);     }          public void actionPerformed(ActionEvent e)     {         if(e.getSource() == addProgressButton)         {             progressbar.setValue(progressbar.getValue()+1);             label.setText(progressbar.getValue() + "% Completed");             label.setSize(label.getPreferredSize());             }     }          JProgressBar progressbar;     JButton addProgressButton;     JLabel label; }
end example

When we execute the example, the following can be seen:

click to expand
Figure 17: The JProgressBar example

Let's now take a look at the code and see how we implemented it. First we create a JProgressBar object called progressbar. This is accomplished with the following line of code:

progressbar = new JProgressBar(JProgressBar.HORIZONTAL, 0, 100);

The first parameter specifies whether you wish the progress bar to be horizontal or vertical. The second and third parameters are the minimum and maximum values, respectively.

The next code of importance is in the actionPerformed method, as we increment the progress when the button is clicked. Here is the section of code that is used to do this:

progressbar.setValue(progressbar.getValue()+1); label.setText(progressbar.getValue() + "% Completed"); label.setSize(label.getPreferredSize());

Therefore, when the button is clicked, we increment the value of our progressbar object by getting the current value of it, adding 1, and then setting this as the new value.



Java 1.4 Game Programming
Java 1.4 Game Programming (Wordware Game and Graphics Library)
ISBN: 1556229631
EAN: 2147483647
Year: 2003
Pages: 237

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net