<Previous    Back to Start  Contents   Next>

1.2 Exam Only: The java.awt package - Event Handling

Write a non-abstract class that implements a specified Listener interface, given the interface definition.

Listener Interfaces
For information and comparison purposes, here is the list of Listener interfaces, and their methods. You do not need to memorize this table. Note the relationship between the Listener interface name, the Event type (which is the argument to the methods) and the 'add' method which belongs to the component classes.

Interface Name Event Methods in interface Add method
ActionListener ActionEvent actionPerformed() addActionListener()
AdjustmentListener AdjustmentEvent adjustmentValueChanged() addAdjustmentListener()
ComponentListener ComponentEvent componentHidden()
ContainerListener ContainerEvent componentAdded()
FocusListener FocusEvent focusGained()
InputMethodListener InputMethodEvent caretPositionChanged()
ItemListener ItemEvent itemStateChanged() addItemListener()
KeyListener KeyEvent keyPressed()
MouseListener MouseEvent mouseClicked()
MouseMotionListener MouseEvent mouseDragged()
TextListener TextEvent textValueChanged() addTextListener()
WindowListener WindowEvent windowActivated()

Select methods from the classes in the java.awt.event package that identify the affected component, mouse position, nature, and time of the event.

The only Event class that has methods for all of the above is MouseEvent.

Affected Component:
There are two methods that fit this description. From looking at the source code, it appears they perform the exact same function.

All ComponentEvent subclasses implement this method:

public Component getComponent()
Returns: the Component object that originated the event

All events implement this method:

public Object getSource()
Returns: the object on which the Event initially occurred.

Mouse Position:
MouseEvent implements these methods:

public Point getPoint()
Returns: a Point object containing the x and y coordinates relative to the source component
public int getX()
Returns: x an integer indicating horizontal position relative to the component
public int getY()
Returns: y an integer indicating vertical position relative to the component

All AWTEvent subclasses implement this method:

public int getID()
Returns the event type. This can be compared with the event mask constants defined in the class for the different types of event e.g. MOUSE_EVENT_MASK.

InputEvent subclasses (KeyEvent and MouseEvent) implement this method:

public long getWhen()
Returns the timestamp of when this event occurred.

Demonstrate correct uses of the Listener methods in the Component, TextArea, and TextField classes.

For examples, see a text book, or the sites at the start of this document. Briefly, you create a class which implements one of the listener interfaces listed above. Important listener interfaces for the components mentioned in this objective are, for example, MouseListener, FocusListener, KeyListener or TextListener.

Create suitable methods for the events you are interested, for the rest of the methods in the interface, just create an empty method body. For example, if you have a button, and you just need it to respond to being clicked, in your listener class you would put code in the mouseClicked() method, and implement the rest of the methods with empty bodies.

Then create an instance of the listener class and add it to the component in question using the components appropriate add method, which is the last column in the table above.

[To save typing, there is the option of using Adapter classes, e.g MouseAdapter for the MouseListener interface. These are classes which have empty methods for all the methods in the Listener. You extend them, and then overwrite the methods you are interested in, saving the effort of typing other methods.]

For any listener in the java.awt.event package, state the argument type and return type of a specified listener method, given the name of the interface that declares the method and the name of the method itself.

The argument types are listed in the "Listener Interfaces" table above, under the "Event" heading. The return type for all methods of all listener interface methods is void.

<Previous    Back to Start  Contents   Next>

©1999, 2000, 2002 Dylan Walsh.
Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.1 or any later version published by the Free Software Foundation; with the Invariant Sections being the disclaimer, no Front-Cover Texts, and no Back-Cover Texts. A copy of the license is included in the section entitled "GNU Free Documentation License".