next up previous
Next: 3.1.3 How to Write Up: 3.1.2 How to Write Previous: 3.1.2.0.1 Warning

3.1.2.0.2 Coding the View Class

The following code defines a view class that supports the schematic display given above:

import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import javax.swing.*;

class ClickCounterView {

  // ** fields **
  private JButton incButton;
  private JButton resetButton;
  private JButton decButton;
  private JLabel valueDisplay;
  
  // ** constructors **
  public ClickCounterView(JApplet itsApplet) {
    JPanel controlPanel = new JPanel();

    itsApplet.getContentPane().setLayout(new BorderLayout());
    valueDisplay = new JLabel("000", JLabel.CENTER);

    itsApplet.getContentPane().add(valueDisplay, "Center");

    incButton = new JButton("+");
    resetButton = new JButton("0");
    decButton = new JButton("-");
    
    controlPanel.add(incButton);
    controlPanel.add(resetButton);
    controlPanel.add(decButton);
    itsApplet.getContentPane().add(controlPanel, "South");
  } 

  // ** methods **
  public void setValueDisplay(String setTo) {
    valueDisplay.setText(setTo);
  }
   
  public void addIncListener(ActionListener a) { 
    incButton.addActionListener(a);
  }

  public void addDecListener(ActionListener a) { 
    decButton.addActionListener(a);
  }

  public void addResetListener(ActionListener a) { 
    resetButton.addActionListener(a);
  }

  public void setMinimumState() {
    incButton.setEnabled(true);
    resetButton.setEnabled(false);
    decButton.setEnabled(false);
  }
    
  public void setCountingState() {
    incButton.setEnabled(true);
    resetButton.setEnabled(true);
    decButton.setEnabled(true);
  }
  
  public void setMaximumState() {
    incButton.setEnabled(false);
    resetButton.setEnabled(true);
    decButton.setEnabled(true);
  }
}

The structure of this program is very simple. Most of the length is due to Java's wordy syntax and long variable names. What does it do?

The Java AWT/Swing library includes a large number of classes for defining graphical components that can be displayed on the screen. The AWT library relies on the window manager of the underlying operating system to implement common graphical components like windows, menus, and buttons. The Swing extension to the AWT library provides ``pure Java'' equivalents of these graphical elements, eliminating the vagaries in graphical style among window systems. For every graphical component class C in the AWT library, the Swing extension includes an equivalent ``pure Java'' component class JC. For example, the AWT library includes a component class Button to represent a button in a graphical window. Hence, the Swing extension includes the corresponding class JButton. With a few exceptions, each Swing component class can be used in place of the corresponding AWT class.

All of the component classes in AWT/Swing are all descendants of the AWT abstract class Component (surprise!). The ClickCounterView class mentions three of these component classes, namely JPanel, JButton, and JLabel which are all subclasses of the Swing abstract class JComponent (which is a subclass of Component). A JPanel object is simply a rectangular region that can be incorporated in a graphical container (such as the JFrame class in the Swing library) which is subsequently displayed on the screen. A panel typically contains other graphical elements (e.g. buttons, drawing canvases, text, pictures) which are displayed as part of the panel. (A blank panel is not very interesting!) A JButton is a graphical button and a JLabel is a single line of text that can be used as a graphical component.

In AWT/Swing library, graphical components that can contain other graphical components are called containers and belong to type Container. Within a container, the layout of the graphical components inside it is determined by a layout manager, a Java object of type LayoutManager. One of the important tasks in programming a user interface is determining which layout manager and combination of parameter values to use. A good layout policy will produce an attractive logical layout for a variety of different frame shapes and sizes.

The layout manager BorderLayout used in ClickCounterView uses compass points to constrain the relative position of graphical components. The four compass points, "North", "East","South", and "West" plus the "Center" position are supported by the layout manager as directives when graphical components are installed within the panel that it manages.

In the program text above, the ClickCounterView object constructor creates panels within the frame provided by the JApplet object that is passed to it. An applet is a ``top level'' container; the browser that is executing the applet provides it with a blank area of the screen in which it can paint its various graphical elements. The constructor for ClickCounterView creates a JLabel displayValue text line to hold the current click count and a JPanel controlPanel containing three buttons, incButton, resetButton, and decButton with adorning String labels +, 0, and -, respectively. The displayValue is placed in the center of the Applet and the three buttons are placed in the controlPanel using the default layout manager FlowLayout. This layout manager places graphical components in rows from left-to-right just like a text editor places characters when you type text into a buffer.

The ClickCounterView class defines seven public methods to access and update its components:


next up previous
Next: 3.1.3 How to Write Up: 3.1.2 How to Write Previous: 3.1.2.0.1 Warning
Corky Cartwright 2002-08-09