Richard G Baldwin (512) 223-4758, baldwin@austin.cc.tx.us, http://www2.austin.cc.tx.us/baldwin/

JDK 1.1, Lightweight Components, Exercising the Lightweight 3D Button Class

Java Programming, Lecture Notes # 178, Revised 02/24/98.

Preface

Students in Prof. Baldwin's Advanced Java Programming classes at ACC are responsible for knowing and understanding all of the material in this lesson.

Introduction

A previous lesson developed and discussed a lightweight 3D button class that mimics a heavyweight button in many respects. That lesson did not provide the code necessary to exercise the lightweight button class. This lesson provides several different applications designed to exercise the lightweight button under different layout managers. This provides the opportunity to see how the button behaves when it is required to take on different sizes and shapes.

The Lightweight Button in a FlowLayout Manager

You will probably need to compile and run this program to appreciate what it does.

This program requires access to the class named LWButton01 which is a lightweight 3D button class.

The purpose of the program is to exercise the lightweight button class named LWButton01 under a FlowLayout manager which attempts to honor the preferred size in both dimensions whenever possible.

The program places two lightweight buttons and a heavyweight button in a Frame object using a FlowLayout manager.

An action listener object is registered on all three of the button objects.

Whenever the user clicks an any of the three buttons, the background color of the Frame object toggles between two different colors.

Since the color of the lightweight buttons is tied to the color of the background, the lightweight buttons also change color when the background color changes. This gives you an opportunity to observe the 3D effect of the highlights and shadows under two different color schemes.

The program confirms proper operation of the setLabel() and getLabel() methods of the LWButton01 class. It also demonstrates the preferredSize aspect of the class as well.

This and the following two programs demonstrate that the lightweight button is much more responsive to rapid mouse clicks than the heavyweight button, at least that is true using JDK 1.1.3 under Win95 on a machine with a 133 mhz Pentium processor.

With the possible exception of an anonymous inner-class WindowListener, the code in this program is very straightforward so there is no need to highlight it in fragments as we often do. The primary purpose of providing this code is to give you an application that you can use to exercise the lightweight button in a FlowLayout manager and observe how it behaves.

Program Listing in the FlowLayout Manager

The program listing follows. Some interesting code fragments are highlighted in boldface.
/* File Lightweight04.java Copyright 1997, R.G.Baldwin
You will probably need to compile and run this program
to appreciate what it does.

This program requires access to the class named LWButton01
which is a lightweight 3D button class.

This program was tested using JDK 1.1 under Win95.
*/
//=======================================================//
import java.awt.*;
import java.awt.event.*;
//=======================================================//
public class Lightweight04 extends Frame {
  public static void main(String[] args){
    new Lightweight04();//instantiate an object of this type
  }//end main

  //-----------------------------------------------------//
  public Lightweight04(){//constructor
    this.setTitle("Copyright 1997, R.G.Baldwin");

    //Set background to a dull color
    this.setBackground(new Color(128,128,0));
    
    //Set the layout to a FlowLayout so that the preferred
    // size of the components will be honored.
    this.setLayout(new FlowLayout());

    //Instantiate some lightweight button objects        
    LWButton01 oneLWButton = new LWButton01("Dick");
    LWButton01 anotherLWButton = new LWButton01();
    
    //Confirm that getLabel() and setLabel() work properly
    anotherLWButton.setLabel(oneLWButton.getLabel() 
                                            + " Baldwin");
    
    //Instantiate a heavyweight button
    Button myButton = new Button("Heavyweight Button");

    //Add the buttons to the container    
    this.add(oneLWButton);    
    this.add(anotherLWButton);    
    this.add(myButton);

    //Instantiate an action listener object and register
    // it on all three buttons.
    MyActionListener myActionListener = 
                             new MyActionListener();
    oneLWButton.addActionListener(myActionListener);
    anotherLWButton.addActionListener(myActionListener);
    myButton.addActionListener(myActionListener);
    
    this.setSize(300,100);
    this.setVisible(true);

    //Anonymous inner-class listener to terminate program
    this.addWindowListener(new WindowAdapter(){
             public void windowClosing(WindowEvent e){
                 System.exit(0);}});//end addWindowListener
  }//end constructor        
}//end class Lightweight04
//=======================================================//
/*Class to respond to action events
This class responds to action events by toggling the
background color of the container of the source of the
event between two different colors.
*/
class MyActionListener implements ActionListener{
  
  Color color1 = new Color(0,128,128);
  Color color2 = new Color(128,128,0);
  
  public void actionPerformed(ActionEvent e){
    Component parent = 
                    ((Component)e.getSource()).getParent();
    if(parent.getBackground().equals(color1))
      parent.setBackground(color2);
    else parent.setBackground(color1);
    parent.invalidate();
    parent.repaint();
  }//end actionPerformed
}//end class MyActionListener
//=======================================================//
From here, we move into the use of the BorderLayout manager with the lightweight button.

The Lightweight Button in a BorderLayout Manager

This program also requires access to the class named LWButton01 which is a lightweight 3D button class.

As before, you will probably need to compile and run the program to really appreciate what it does.

The purpose of the program is to exercise the lightweight button class named LWButton01 under a BorderLayout manager.

The BorderLayout manager does not simultaneously honor both dimensions of the preferredSize of a component in any of the five positions of the border layout. Components in the North and South positions probably have the vertical dimension of their preferredSize honored (when possible). If you resize the container and make it too small in either dimension, it is probably not possible to honor the preferredSize in that dimension.

Similarly, components in the East and West positions probably have the horizontal dimension of their preferredSize honored (when possible).

Neither dimension of the preferredSize is honored for components in the Center. Components in the Center simply occupy all of the available space that is left over from placing components in the other four positions..

This program places three lightweight buttons of type LWButton01, one heavyweight button of type Button, and a Label object in a Frame object with a ten-pixel gap between all components.

The lightweight buttons occupy the East, North, and Center positions in the Frame.

The heavyweight button occupies the West position.

The Label occupies the South position.

When you click on any of the four buttons, the color of the Label toggles between red and green.

You can experiment with the manner in which the preferredSize is or is not honored by resizing the Frame.

Note that this program makes use of a named inner class for the ActionListener object. Use of a named inner class for this purpose makes it possible for code in the listener object to refer directly to the Label object to toggle its color without the requirement to pass parameters to the constructor, or disassemble the ActionEvent object in an attempt to gain access to the Label. That makes it very convenient to use.

The program also uses an anonymous inner class to service the close button on the Frame object.

All of the code in this program has been discussed in earlier lessons. Therefore, we won't break it up into fragments, but will simply highlight interesting parts using boldface in the following program listing.

This program was tested using JDK 1.1.3 under Win95.

Program Listing in the BorderLayout Manager

The complete program listing along with highlighted fragments follows.
/* File Lightweight05.java Copyright 1997, R.G.Baldwin
This program requires access to the class named LWButton01
which is a lightweight 3D button class.

This program was tested using JDK 1.1.3 under Win95.
*/
//=======================================================//
import java.awt.*;
import java.awt.event.*;
//=======================================================//
public class Lightweight05 extends Frame{
  Label myLabel;
  
  public static void main(String[] args){
    new Lightweight05();//instantiate object of this type
  }//end main

//-------------------------------------------------------//
  public Lightweight05(){//constructor
    this.setTitle("Copyright 1997, R.G.Baldwin");
    //Set background to a dull yellow
    this.setBackground(new Color(128,128,0));
    
    //Create a borderLayout object with gaps and apply
    // it to the Frame object.
    BorderLayout myLayout = new BorderLayout();
    myLayout.setHgap(10);
    myLayout.setVgap(10);
    this.setLayout(myLayout);    
    
    //Instantiate three lightweight buttons
    LWButton01 eastLWButton = new LWButton01("East");
    LWButton01 northLWButton = new LWButton01("North");
    LWButton01 centerLWButton = new LWButton01("Center");
    
    //Instantiate a Label object and initialize it to green
    myLabel = new Label("Label Object");
    myLabel.setBackground(Color.green);
    
    //Instantiate a heavyweight button object
    Button myButton = new Button("Heavyweight Button");
    
    //Add all five components to the Frame object.
    this.add(eastLWButton,"East");    
    this.add(northLWButton,"North");    
    this.add(centerLWButton,"Center");
    this.add(myButton,"West");
    this.add(myLabel,"South");

    //Instantiate an ActionListener object
    MyActionListener myActionListener = 
                                   new MyActionListener();
                             
    //Register the ActionListener object on all four
    // of the buttons.                             
    eastLWButton.addActionListener(myActionListener);
    northLWButton.addActionListener(myActionListener);
    centerLWButton.addActionListener(myActionListener);
    myButton.addActionListener(myActionListener);
    
    this.setSize(300,200);
    this.setVisible(true);

    //Anonymous inner-class listener to terminate program
    this.addWindowListener(new WindowAdapter(){
               public void windowClosing(WindowEvent e){
                 System.exit(0);}});//end addWindowListener
  }//end constructor
  //-----------------------------------------------------//
  
  //Named inner Class to respond to action events. Make 
  // this an inner class for easy access to myLabel.
  class MyActionListener implements ActionListener{
    public void actionPerformed(ActionEvent e){
      if(myLabel.getBackground() == Color.green)
        myLabel.setBackground(Color.red);
      else myLabel.setBackground(Color.green);
    }//end actionPerformed
  }//end class MyActionListener
}//end class Lightweight05
//=======================================================//
The primary purpose of providing this program is to give you a vehicle for experimenting with the lightweight button in a BorderLayout manager, while paying particular attention to how the lightweight button gets resized and reshaped as you change the size and shape of the Frame object.

The Lightweight Button in a GridLayout Manager

As with the previous two programs, this program requires access to the class named LWButton01 which is a lightweight 3D button class.

Again, you will probably need to compile and run the program to really appreciate what it does.

The purpose of the program is to exercise the lightweight button class named LWButton01 under a GridLayout manager.

The GridLayout manager does not honor either dimension of the preferredSize of a component. Rather, all components are displayed in a grid at the same size.

This program places four lightweight buttons of type LWButton01, one heavyweight button of type Button, and a Label object in a Frame object with a ten-pixel gap between all components.

When you click on any of the five buttons, the color of the Label toggles between red and green.

You can experiment with the fact that the preferredSize is not honored by resizing the Frame and observing how all of the components change their size and shape.

As in the previous program, this program uses a named inner class to implement an ActionListener object in order to make the reference to the Label object more readily accessible.

It also uses an anonymous inner class to service the close button on the Frame object. Otherwise, there is nothing in this program that we haven't seen many times before.

Program Listing in the GridLayout Manager

Some of the interesting parts of the following program listing are highlighted in boldface.
/* File Lightweight06.java Copyright 1997, R.G.Baldwin
This program requires access to the class named LWButton01
which is a lightweight 3D button class.

This program was tested using JDK 1.1.3 under Win95.
*/
//=======================================================//
import java.awt.*;
import java.awt.event.*;
//=======================================================//

public class Lightweight06 extends Frame{
  Label myLabel;  
  
  public static void main(String[] args){
    new Lightweight06();//instantiate object of this type
  }//end main
//-------------------------------------------------------//

  public Lightweight06(){//constructor
    this.setTitle("Copyright 1997, R.G.Baldwin");
    //Set background to a dull yellow
    this.setBackground(new Color(128,128,0));
    
    //Create a gridLayout object with 10-pixel gaps and 
    // apply it to the Frame object; 3 rows, 2 columns.
    GridLayout myLayout = new GridLayout(3,2,10,10);
    this.setLayout(myLayout);    
    
    //Instantiate three lightweight buttons, one with a
    // very long label to illustrate what happens to long
    // labels in the GridLayout where preferred size is
    // not honored.
    LWButton01 oneLWButton = new LWButton01("Joe");
    LWButton01 twoLWButton = new LWButton01("William");
    LWButton01 threeLWButton = new LWButton01("Alexandria");
    LWButton01 fourLWButton = 
             new LWButton01("My name is a very long label");
    
    //Instantiate a Label object and initialize it to green
    myLabel = new Label("Label Object");
    myLabel.setBackground(Color.green);
    
    //Instantiate a heavyweight button object
    Button myButton = new Button("Real Button");
    
    //Add all six components to the Frame object.
    this.add(oneLWButton);    
    this.add(twoLWButton);    
    this.add(threeLWButton);
    this.add(fourLWButton);
    this.add(myButton);
    this.add(myLabel);

    //Instantiate an ActionListener object
    MyActionListener myActionListener = 
                                   new MyActionListener();
                             
    //Register the ActionListener object on all five
    // of the buttons.                             
    oneLWButton.addActionListener(myActionListener);
    twoLWButton.addActionListener(myActionListener);
    threeLWButton.addActionListener(myActionListener);
    fourLWButton.addActionListener(myActionListener);    
    myButton.addActionListener(myActionListener);
    
    this.setSize(300,200);
    this.setVisible(true);

    //Anonymous inner-class listener to terminate program
    this.addWindowListener(new WindowAdapter(){
               public void windowClosing(WindowEvent e){
                 System.exit(0);}});//end addWindowListener
  }//end constructor
  
  //-----------------------------------------------------//
  //Named inner class to respond to action events.  Make 
  // this an inner class for easy access to myLabel.
  class MyActionListener implements ActionListener{       
    public void actionPerformed(ActionEvent e){
      if(myLabel.getBackground() == Color.green)
        myLabel.setBackground(Color.red);
      else myLabel.setBackground(Color.green);
    }//end actionPerformed
  }//end class MyActionListener
  
}//end class Lightweight06
//=======================================================//
-end-