Application Techniques

Using the JProgressBar Control

How to customize the appearance of a JProgressBar control.

About this technique



Java Client Techniques> Version 3 Swing-based controls


You'll learn about:

You can run this technique code from:

NOTE   First make sure that database is running on your localhost SilverStream Server

Related reading

For another example of:

JProgressBar is part of the JDK and is not extended by SilverStream. In the Form Designer, it is on the Other toolbar. The example frmJProgressMeter has a timer that "ticks" every .3 seconds and increments the value of the progress bar by 5 on each tick. The form also has several controls that let the user change the appearance of the progress bar as well as start and stop the timer.

Specifying the control's size and position   Top of page

In frmJProgressMeter, the user can choose a vertical or horizontal progress bar. During initialization, code in the formActivate event saves the initial position and size of the horizontal progress bar in instance variables.

  // Get the JProgress bar location and size 
  Point point = jProgressBar.getLocation(); 
  m_xPosition = point.x; 
  m_yPosition = point.y; 
  Dimension dimension = jProgressBar.getSize(); 
  m_horizontalWidth = dimension.width; 
  m_horizontalHeight = dimension.height; 

When the user selects vertical orientation, the code for the button's actionPerformed event changes the orientation. It also changes the width and height to a tall, thin shape.

  private void handle_rbVertical_actionPerformed(ActionEvent evt) 
     jProgressBar.setOrientation (JProgressBar.VERTICAL); 
     jProgressBar.setBounds (m_xPosition, m_yPosition, 
        m_verticalWidth, m_verticalHeight); 

Notes on the code

Selecting colors   Top of page

Two image buttons let the user choose a background and foreground color for the progress bar. The code for the background button's actionPerformed event uses the JDK's color selection dialog box.

  private void handle_imgBackgroundColor_actionPerformed(ActionEvent evt) 
     Color colorBack = JColorChooser.showDialog (getParent(), 
        "Progress Bar Background", jProgressBar.getBackground()); 
     jProgressBar.setBackground (colorBack); 

Using the timer   Top of page

The user starts and stops the timer via buttons on the form. The timer has not been set to start automatically. This code starts and stops the timer.

  private void handle_imgStart_actionPerformed(ActionEvent evt) 
     jProgressBar.setValue (0); 
  private void handle_imgStop_actionPerformed(ActionEvent evt) 

This code updates the progress bar after each timer interval.

  private void handle_tmrProgressBar_timeIntervalExpired(EventObject event) 
     jProgressBar.setValue (jProgressBar.getValue() + 5); 
     if (jProgressBar.getValue() >= 100) jProgressBar.setValue (0); 

Responding to changes in the progress bar's value   Top of page

The stateChanged event for the progress bar is fired when the value of the progress bar changes. It is not automatically available in the Form Designer. There are three parts to using it in this example:

Notes on the code

Copyright © 2000, SilverStream Software, Inc. All rights reserved.