Main Page

Previous Next

Creating a Status Bar

One limitation of the Sketcher program as it stands is that you have no direct feedback on what current element type and color have been selected. As a gentle start to this chapter, let's fix that now. A window status bar is a common and very convenient way of displaying the status of various application parameters, each in its own pane.

We can make up our own class, StatusBar for instance, that will define a status bar. Ideally we would design a class for a generic status bar and customize it for Sketcher, but we will take the simple approach of designing a class that is specific to Sketcher. The JPanel class would be a good base for our StatusBar class since it represents a panel, and we can add objects representing status bar panes to it. We can use the JLabel class as a base for defining status bar panes and add sunken borders to them for a distinct appearance.

Let's start with a status bar at the bottom of Sketcher with two panes to show the current element type and color. Then we will know exactly what we are about to draw. We can start by defining the StatusBar class that will represent the status bar in the application window, and we'll define the StatusPane class as an inner class to StatusBar.

Try It Out – Defining a Status Bar Class

Here's an initial stab at the definition for the StatusBar class:

// Class defining a status bar
import javax.swing.*;
import javax.swing.border.BevelBorder;
import java.awt.*;

class StatusBar extends JPanel implements Constants {
  // Constructor
  public StatusBar() {
    setLayout(new FlowLayout(FlowLayout.LEFT, 10, 3));
    add(colorPane);                      // Add color pane to status bar
    add(typePane);                       // Add type pane to status bar

  // Set color pane label
  public void setColorPane(Color color) {
  // Code to set the color pane text...

  // Set type pane label
  public void setTypePane (int elementType) { 
  // Code to set the type pane text....

  // Panes in the status bar
  private StatusPane colorPane = new StatusPane("BLUE");
  private StatusPane typePane = new StatusPane("LINE");

  // Class defining a status bar pane
  class StatusPane extends Jlabel {
    public StatusPane(String text) {
      setBackground(Color.light_Gray);      // Set background color
      setFont(paneFont);                   // Set the fixed font
      setHorizontalAlignment(CENTER);      // Center the pane text 
      setPreferredSize(new Dimension(100,20));
      setText(text);                       // Set the text in the pane

    // Font for pane text
    private Font paneFont = new Font("Serif", Font.PLAIN, 10);

How It Works

Since the StatusBar class implements our Constants interface, all the variables that represent possible element types and colors are available. This outline version of StatusBar has two data members of type StatusPane, which will be the panes showing the current color and element type. The initial information to be displayed by a StatusPane object is passed to the constructor as a String object.

In the StatusBar constructor, we update the information to be displayed in each pane by calling the setColorPane() and setTypePane() methods. These ensure that initially the StatusPane objects display the default color and type that we've defined for the application. One or other of these methods will be called whenever it is necessary to update the status bar. We'll complete the definitions for setColorPane() and setTypePane() when we've been through the detail of the StatusPane class.

The StatusBar panel has a FlowLayout manager that is set in the constructor. The panes in the status bar need only display a small amount of text, so we've derived the StatusPane class from the JLabel class – so a pane for the status bar will be a specialized kind of JLabel. This means that we can call the setText() method that is inherited from JLabel to set the text for our StatusPane objects. The StatusPane objects will be left-justified when they are added to the status bar, as a result of the first argument to the setLayout()method call in the StatusBar constructor. The layout manager will leave a ten-pixel horizontal gap between successive panes in the status bar, and a three-pixel vertical gap between rows of components. The border for the status bar is a single dark gray line that we add using the BorderFactory method.

The only data member in the StatusPane class is the Font object, font. We've defined the font to be used for pane text as a standard 10-point Serif. In the constructor we set the background color to light gray, the foreground color to dark gray, and we set the standard font. We also set the alignment of the text as centered by calling the inherited method setHorizontalAlignment(), and passing the value CENTER to it – this is defined in the base class, JLabel.

If we can maintain a fixed width for each pane, it will prevent the size of the pane jumping around when we change the text. So we've set the setPreferredSize() at the minimum necessary for accommodating our longest text field. Lastly, in the StatusPane constructor we set the text for the pane by calling the inherited setText() method.

Try It Out – Updating the Panes

We can code the setColorPane() method as:

// Set color pane label
public void setColorPane(Color color) {
  String text = null;                              // Text for the color pane
    text = "RED";

  else if(color.equals(Color.yellow))
    text = "YELLOW";
  else if(color.equals(
    text = "GREEN";
  else if(color.equals(
    text = "BLUE";
    text = "CUSTOM COLOR";
  colorPane.setText(text);                         // Set the pane text

In the code for the setTypePane()method we can use switch rather than if statements to test the parameter value because it is of type int:

// Set type pane label
public void setTypePane(int elementType) { 
  String text = null;                           // Text for the type pane
  switch(elementType) {
    case LINE:
      text = "LINE";
    case RECTANGLE:
      text = "RECTANGLE";
    case CIRCLE:
      text = "CIRCLE";
    case CURVE:
      text = "CURVE";
      assert false;
  typePane.setText(text);                       // Set the pane text

How It Works

This code is quite simple. The text to be displayed in the color pane is selected in the series of if-else statements. They each compare the color passed as an argument with the standard colors we use in Sketcher and set the text variable accordingly. The last else should never be reached at the moment, but it will be obvious if it is. This provides the possibility of adding more flexibility in the drawing color later on. Note that we also set the foreground color to the currently selected element color, so the text will be drawn in the color to which it refers.

The type pane uses a switch as it is more convenient but the basic process is the same as for the color pane. If something goes wrong somewhere that results in an invalid element type, the program will assert through the default case.

All we need now is to implement the status bar in the SketchFrame class. For this we must add a data member to the class that defines the status bar, add the status bar to the content pane of the window in the class constructor, and extend the actionPerformed() methods in the TypeAction and ColorAction classes to update the status bar when the element type or color is altered.

Try It Out – The Status Bar in Action

You can add the following statement to the SketchFrame class to define the status bar as a data member, following the members defining the menu bar and toolbar:

private StatusBar statusBar = new StatusBar();    // Window status bar

We create statusBar as a data member so that it can be accessed throughout the class definition, including from within the Action classes. You need to add one statement to the end of the SketchFrame class constructor:

public SketchFrame(String title, Sketcher theApp) {
  // Constructor code as before...
  getContentPane().add(statusBar, BorderLayout.SOUTH);        // Add the statusbar

This adds the status bar to the bottom of the application window. To update the status bar when the element type changes, you can add one statement to the actionPerformed() method in the inner class, TypeAction:

public void actionPerformed(ActionEvent e) {
  elementType = typeID;

The type pane is updated by calling the setTypePane() method for the status bar and passing the current element type to it as an argument. We can add a similar statement to the actionPerformed() method to update the color pane:

public void actionPerformed(ActionEvent e) {
  elementColor = color;

If you now recompile and run Sketcher again, you'll see the status bar in the application.

As you change the element type and color, the status bar will be updated automatically.

Previous Next
JavaScript Editor Java Tutorials Free JavaScript Editor