Status Bar


Summary:

A status bar is a horizontal window at the bottom of a parent window in which an application can display various kinds of status information. It is typically seen in such applications where background processing is taking place and notification of such action is relayed to the user. Some applications are a ticker of stock prices and headlines, internet navigators displaying of URL information, or a word processing program background printing a document.

Where to Find It:

While no actual component in Java exists for the Status Bar, simple Java code can be used to "simulate" one:

// Declare a variable "hintBar" and initialize it to read "Ready..."
JLabel hintBar = new JLabel ("Ready...");

// Set the border of the Label to be a beveled (slightly 3D out dented)
hintBar.setBorder( BorderFactory.createLoweredBevelBorder() );

// Add the label to the frame to the South (when using the BorderLayout in the Frame)
frame.getContentPane().add( hintBar, BorderLayout.SOUTH );

Simply put, the status bar that we are using is a label sitting at the bottom of the screen.  When you want to change it, it is suggested that you use a manager of some sort that stores and controls the messages on the display.  This is described in greater detail in the Customization section below.

In Microsoft .NET languages, such as C# and C++, there exists a separate StatusBar class.  This is found in the package System.Windows.Forms.StatusBar .  This class is a storage member of various StatusBarPanel objects.  To create one that has similar behavior as the Java code above, the following code should be used:

private void CreateMyStatusBar()
{

// Create a StatusBar control.
StatusBar statusBar1 = new StatusBar ();

// Create a StatusBarPanel object to display in the StatusBar.
StatusBarPanel panel1 = new StatusBarPanel ();

// Display the first panel with a sunken border style.
panel1.BorderStyle = StatusBarPanelBorderStyle.Sunken;

// Initialize the text of the panel.
panel1.Text = "Ready...";

// Set the AutoSize property to use all remaining space on the StatusBar.
panel1.AutoSize = StatusBarPanelAutoSize.Spring;

// Display panels in the StatusBar control.
statusBar1.ShowPanels = true;

// Add both panels to the StatusBarPanelCollection of the StatusBar.           
statusBar1.Panels.Add(panel1);

// Add the StatusBar to the form.
this.Controls.Add (statusBar1);

}

The Basics:

           

            Figure 1: Sample Window with Status Bar

Many programmers find the status bar more useful than a tool tip for displaying information regarding a component for a variety of reasons:

1.      Tool tips are appropriate just for very small hints, since the Swing implementation does not break the text.

2.      You must wait until the tool tip is displayed and read it before it's gone.

3.      Hints are somewhat intrusive since they are displayed over the component. For that reason, they don't look good on some components such as menu items.

Customization:

The ideal way of utilizing the Status Bar in Java, as it is just a JLabel at the bottom of the screen, would be to design your program with a separate manager that controls the text currently on the bar.  In the below code example, the Manager holds onto the Status Bar and listens to various components within the UI.  When a Mouse goes over the component, the Manager simply looks up in the HashMap what should be placed into the JLabel and updates accordingly.

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;
 

 

public class MouseOverHintManager implements MouseListener {
  private Map hintMap;
 
  // A local reference to the Status Bar that we will be updating
  private JLabel statusBar;
 
  public MouseOverHintManager( JLabel statusBar ) {
    hintMap = new WeakHashMap();
    this.statusBar = statusBar;
  }
 
  public void addHintFor( Component comp, String hintText ) {
    hintMap.put( comp, hintText );
  }
 
  public void enableHints( Component comp ) {
    comp.addMouseListener( this );
    if ( comp instanceof Container ) {
      Component[] components = ((Container)comp).getComponents();
      for ( int i=0; i<components.length; i++ )
       enableHints( components[i] );
    }
    if ( comp instanceof MenuElement ) {
      MenuElement[] elements = ((MenuElement)comp).getSubElements();
      for ( int i=0; i<elements.length; i++ )
       enableHints( elements[i].getComponent() );
    }
  }
 
  private String getHintFor( Component comp ) {
    String hint = (String)hintMap.get(comp);
    if ( hint == null ) {
      if ( comp instanceof JLabel )
       hint = (String)hintMap.get(((JLabel)comp).getLabelFor());
      else if ( comp instanceof JTableHeader )
       hint = (String)hintMap.get(((JTableHeader)comp).getTable());
    }
    return hint;
  }
 

 

  public void mouseEntered( MouseEvent e ) {
    Component comp = (Component)e.getSource();
    String hint;
    do {
      hint = getHintFor(comp);
      comp = comp.getParent();
    } while ( (hint == null) && (comp != null) );
    if ( hint != null )
      statusBar.setText( hint );
  }
 
  public void mouseExited( MouseEvent e ) {
    statusBar.setText( " " );
  }
 
  public void mouseClicked( MouseEvent e ) {}
  public void mousePressed( MouseEvent e ) {}
  public void mouseReleased( MouseEvent e ) {}
}

Best Practices:

Ensure that only one entity manipulates or changes the Status Bar, rather than each component doing it separately.  This ensures adequate separation of concerns and will reduce the coupling that will adversely effect the maintainability and expandability of your program.  It's just good software engineering.

Things to Watch Out for When Including Status Bar:

The only major issue related to the Status Bar may be related to how fast you update the text on the screen.  The "flicker" effect that tends to occur when text is updated greater than 10 times per second will certainly be more evident with interpreted programs like Java where the overhead of the Virtual Machine is high.  Therefore, keep in mind that in some applications (perhaps displaying data from a probe or sensor) may need some other implementation method where a buffer can be used to minimize the flickering effect.

References:

Other Helpful Resources: