Interface V
Home Up Interface I Interface II Interface III Interface IV Interface V Interface VI Interface VII Interface VIII Interface IX Interface X Interface XI Interface XII Code

 

The object of today's lesson is to introduce Java's event handling model.   Experiment with the applet below to see what it does.  Then proceed to understand the code that generates the applet.

An applet would appear here if your browser supported Java.

In addition to the "awt" and "applet" libraries, this applet also includes the "awt.event" library.  The "awt.event" library includes all of the event handling support.  The applet declares two variables, each of type "Button" at the very bottom of the program.  A "Button" is one of Java's numerous predefined user interface components.  Anytime a "Button" is clicked on, an event (that can potentially be handled) is generated.   Would you like to learn more about buttons?

Notice that this particular applet "implements ActionListener".   "ActionListener" is an interface that requires the method "actionPerformed" to be implemented.  (Recall that in the application phase of this tutorial, we used an "interface" for organizing constants.  Another use of an "interface" is to supply abstract methods that must be implemented when the interface is used.)  "actionPerformed" is the method that is automatically invoked anytime a "Button" is clicked on.  Do you want to see a summary of event handling interfaces?

Take a look at the "init" method.  The method begins by setting the background to a light blue and by setting the layout manager to be the null layout manager.  The "lightGrayButton" is now created and given a label of "lightGray".  Since a "Button" is an example of a "Component", all of the methods available to a "Component" are available to a "Button".  Two of these methods are "setBackground" and "setForeground".  The background color of "lightGrayButton" is changed to a "lightGray" and the color of its label is changed to "white".  "setBounds" is another method available to a "Component" instance.  It specifies the upper left x and y coordinates of the component, as well as its width and height.  Using the null layout manager, all components default to having widths and heights of 0 so it is critical for the user to reshape components when using the null layout manager.   The "lightGrayButton" is now added to the current display using whatever x, y, width, and height values were specified by the "setBounds" method.   Finally, the "addActionListener" method is invoked so that the user can define what needs to be done in the event that the "Button" is clicked upon.

The final six statements in the "init" method add a "darkGrayButton" to the display in an analagous manner. 

Exercise:  The null layout manager gives you the most control over the appearance of the display.  Eliminate (comment out) the "setLayout(null);" statement so that the flow layout manager will be used.   The "setBounds" method only works in conjunction with the null layout manager, so comment out the two "setBounds" calls also.  Now recompile and run the program to see what happens.  I recommend always using the null layout manager.  However, if you want to rely on the layout manager to place the components for you, you now know how to do it.

The "actionPerformed" method must be defined since the applet implements "ActionListener" (see the first line of the code after the "import" statements).  The method takes a parameter of type "ActionEvent" and is automatically called anytime the event of a button being clicked (for this particular example) takes place.  The method first calls the "getActionCommand" method, which returns the label of the "Button" that invoked the "actionPerformed" method in the first place.  Depending on which "Button" generated the event, the background color is changed to the appropriate color and then the "repaint" method is called to cause the display to be regenerated (and the new background color to show up).

import java.awt.*;
import java.awt.event.*;
import java.applet.*;              
public class Example extends Applet implements ActionListener
{
	public void init()
	{		
		setBackground(new Color(0, 200, 200));              
		setLayout(null);              
		lightGrayButton = new Button ("lightGray");
		lightGrayButton.setBackground(Color.lightGray);
		lightGrayButton.setForeground(Color.white);
		lightGrayButton.setBounds(100, 50, 100, 100);
		add (lightGrayButton );
		lightGrayButton.addActionListener(this);              
		darkGrayButton = new Button ("darkGray");
		darkGrayButton.setBackground(Color.darkGray);
                darkGrayButton.setForeground(Color.white);
		darkGrayButton.setBounds(300, 50, 100, 100);
		add (darkGrayButton);
		darkGrayButton.addActionListener(this);
	}              
	public void actionPerformed (ActionEvent evt)
	{
		String arg = evt.getActionCommand();              
		if (arg.equals("darkGray"))
			setBackground(Color.darkGray);
		else if (arg.equals("lightGray"))
			setBackground(Color.lightGray);              
		repaint();
	}              
    private Button lightGrayButton;
    private Button darkGrayButton;              
}