Return to index

Part 2

Next we will add another object to respond to the button click. We are going to use a 'light' - a software equivalent of a ceiling light or table lamp or control panel bulb.

There is nothing quite like a 'light' in the Java development kit so we should start by defining the public interface that such objects should have. In doing so we should stick to the essentials. A reasonable interface would be the following. It defines two (related) methods that must be implemented.

package projects.guicomps;

 

public interface Light {

public void setOn(boolean b);

public boolean isOn();

}

 

We will base our light class on class JComponent, the base class for the 'Swing' GUI component class hierarchy, because there is not a more suitable subclass. Class JComponent provides us with many GUI component features that we will not need to worry about providing - such as a tooltip - but most importantly a framework for being arranged and painted correctly.

The two things we must define for a GUI component are the component's size and how to paint it on the screen. A layout manager will ask a light object (and all the other GUI objects within its range) for its size by calling method getPreferredSize() which returns an object of class Dimension (this contains the two key data values for width and height). Whenever the screen needs to be painted or repainted, method paint(Graphics g) will be called. The Graphics object 'g' does the actual painting, so for the light object to paint itself it will call various methods defined by the Graphics class, supplying dimensions and colors etc as 'parameters'.

We will call out light class 'LightBox'. Class LightBox extends JComponent and implements interface Light and interface ActionListener.

Here is the complete source code for the class:

 

package projects.guicomps;

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

 

public class LightBox extends JComponent implements Light, ActionListener {

boolean on;

 

// Constructor

public LightBox() {

on=false; // light is off at start

}

 

public Dimension getPreferredSize() {

return new Dimension(15,15);

}

 

public void paint(Graphics g) {

Color c=Color.darkGray; //off

if (on) {

c=Color.red; //on

}

g.setColor(c);

g.fillRect(0,0,14,14);

g.setColor(Color.black);

g.drawRect(0,0,14,14);

}

 

public void setOn(boolean b) {

on=b;

repaint();

}

 

public boolean isOn() {

return on;

}

 

public void actionPerformed(ActionEvent e) {

String s=e.getActionCommand();

if (s.equals("On")) {

on=true;

}

if (s.equals("Off")) {

on=false;

}

}

}

 

Now, how do we get the two objects - the button and the light - to interact. That is, how to get the light to respond to the button?

To do this, we define class LightBox as a listener for ActionEvents by declaring that it implements the ActionListener interface; this defines the method that will be called when such an event occurs in a class that is being listened to (public actionPerformed(ActionEvent)). Then we add the LightBox object as an action listener to the JButton object, with this line:

button.addActionListener(light);

What this means is: 'when the button is clicked, call the method actionPerformed(ActionEvent) in object 'light'.

A 'message' is a method call to another object. When an event is sent to a listener, this is also a method call.

The event could be sent to a number of 'listeners'. Every listener added is kept in a list and the actionPerformed(ActionEvent) method of each is called when the button is clicked. All this is taken care of by class JButton.

Our new example class has three more statements than the corresponding code in Example1. The first is to create an object of class LightBox. A second is to add the LightBox object as a listener of the JButton object. And the third is to add the light component to our JPanel object.

Here is the complete source code for class Example2:

 

package projects.example2;

import javax.swing.JButton;

public class Example2 extends JPanel {

JButton button;

LightBox light;

 

// Constructor

public Example2() {

button=new JButton("On");

light=new LightBox();

button.addActionListener(light);

add(button);

add(light);

}

}

 

Class Example2 extends JPanel, which is a GUI container. The following statements:

add(button);

add(light);

simply add the GUI components button and light to our Example2 object, which is based on class JPanel.

Class LightBox is a class that we could use again in other programs - and indeed we do use it again in other example programs - so we will put it in a different package, called projects.guicomps

In class Example2 we declare that we will use class projects.guicomps.LightBox by including the following import statement:

import projects.guicomps.*;

 

Run Example2 to confirm that the button does indeed switch on the light.

In class LightBox, we allowed for the light to be switched on and off - by checking the ActionCommand property of the ActionEvent. The ActionCommand property is a String that can be retrieved from the ActionEvent object by calling method String getActionCommand()

Here is a reminder of the actionPerformed method in class LightBox. Remember that this method is defined by the ActionListener interface that any class that needs to listen to ActionEvents must implement.

public void actionPerformed(ActionEvent e) {

String s=e.getActionCommand();

if (s.equals("On")) {

on=true;

}

if (s.equals("Off")) {

on=false;

}

}

 

In the next section we look at different ways to switch the light both on and off.

Download and run ...

You can download the source code for the example programs as a TJI project from the 'Resources' page of our website.

 

(c) Kinabaloo Software

Return to index