[ Overview ] [ User's Guide ] [ Tutorial ] [ Requirements ]

ProgressBar Bean

Tutorial   API Documentation

Using the Taligent ProgressBar Widget is incredibly easy. This document shows you how to build the following simple progress bar to introduce you to how you can use the ProgressBar Widget in your own programs:

[Screen capture of applet]

Click on the progress bar to change its style.

Note: As of this publication, no commercial web browsers support JDK 1.1. Therefore, this sample applet using the ProgressBar will not work in current versions of Netscape Navigator, Internet Explorer, and other web browsers. If you are using Sun's new HotJava browser or the JDK 1.1 AppletViewer, use the URL link of the image below.

This tutorial describes how to create this applet in three steps:

  1. Building a Progress Bar
  2. Making it Move
  3. Changing the Settings

See the API Documentation for a complete list of the methods and features provided by the ProgressBar Bean.


Building a Progress Bar

To create a standard progress bar with default settings, just create a new instance:

  ProgressBar progress = new ProgressBar();

A default ProgressBar has the following settings:

ProgressBar also provides these constructors that let you specify either a style or the minimum and maximum values:

 void ProgressBar(int style);
 void ProgressBar(int min, int max);

You can also modify any of the settings at runtime. See Changing the Settings below.

Making It Move

Once you have created a ProgressBar, you need to make it move. To do this, update the bar after completing each action that the progress bar is showing.

Use the following ProgressBar methods to set the value that the progress bar will display.

This example demonstrates a simple applet that contains a standard progress bar and uses a Thread to make it move automatically:

import java.awt.*;
import java.awt.event.*;

import COM.taligent.widget.*;

public class SampleProgressBar
    extends java.applet.Applet
    implements Runnable, MouseListener {
    // Data
    ProgressBar progress = new ProgressBar();
    Thread thread;

    // Set up the applet
    public void init() {

        setBackground(Color.lightGray);
        setLayout(new BorderLayout());

        // add components
        BorderPanel border = new BorderPanel(BorderPanelStyle.LOWERED);
        border.setLayout(new BorderLayout());
        border.setGap(0);
        border.add("Center", progress);

        add("Center", border);

        // add listeners
        progress.addMouseListener(this);

        setSize(150, 30);
        setVisible(true);
        }


    // Start the applet
    public void start() {
        thread = new Thread(this);
        thread.start();
    }

    // Stop the applet
    public void stop() {
        if (thread != null) {
            thread.stop();
            thread = null;
        }
    }

    // Make it move!
    public void run() {
        progress.reset();
        while (progress.getValue() < progress.getMaximum()) {
            try {
                // update progress bar
                progress.add((int)(Math.random() * 20));

                // pause a bit to catch our breath
                thread.sleep(333);
            }
            catch (InterruptedException e) {
                // I've been interrupted!
                break;
            }
        }
    }

    // Border applet
    public Insets insets() {
        return new Insets(2, 2, 2, 2);
    }
}

Compile the code above and you'll have a simple progress bar! The next sections shows you how to change the style of the progress bar whenever the user clicks on the bar.

Changing the Settings

You can change the settings of the progress bar at any point during run-time. To do so, simply call the following methods:

Note: You must call start() and stop() to start and stop a striped progress bar. Once started, a striped progress bar moves until stopped.

This code causes the bar to change its style when the user clicks on it:

    // Data
    int    mode;

    // Mouse event handler
    public void mousePressed(MouseEvent evt) {}
    public void mouseReleased(MouseEvent evt) {}
    public void mouseEntered(MouseEvent evt) {}
    public void mouseExited(MouseEvent evt) {}

    public void mouseClicked(MouseEvent evt) {
        Object source = evt.getSource();

        if (source == progress) {
            if (thread != null)
                thread.stop();

            mode = (mode + 1) % 7;
            switch (mode) {
                case 0:
                    progress.setStyle(ProgressBarStyle.SOLID);
                    progress.setDirection(ProgressBarDirection.RIGHT);
                    break;
                case 1:
                    progress.setPercent(true);
                    break;
                case 2:
                    progress.setPercent(false);
                    progress.setDirection(ProgressBarDirection.LEFT);
                    break;
                case 3:
                    progress.setStyle(ProgressBarStyle.BLOCK);
                    progress.setDirection(ProgressBarDirection.RIGHT);
                    break;
                case 4:
                    progress.setDirection(ProgressBarDirection.LEFT);
                    break;
                case 5:
                    progress.setStyle(ProgressBarStyle.STRIPED);
                    progress.setDirection(ProgressBarDirection.RIGHT);
                    break;
                case 6:
                    progress.setDirection(ProgressBarDirection.LEFT);
                    break;
                }

            if (progress.getStyle().isStriped()) {
                if (thread != null) {
                    thread.stop();
                    thread = null;
                    }
                progress.start();
                }
            else {
                progress.stop();
                thread = new Thread(this);
                thread.start();
                }
            }
        }

Copyright © Taligent, Inc. 1996 - 1997.
Copyright
© IBM Corporation 1996 - 1997.
All Rights Reserved.