Event Handling

All of our examples have been write only: displaying GUI objects on a window or panel, but with no means of the user communicating back to the program. This communication is provided by the Swing event-handling mechanism.

The basic idea of event handling is to register an object to act as a listener for a particular type of event on a particular component. Typically, the listener object will be the applet or application instance. An event, such as clicking on a component, causes an event object to be sent to all registered listeners. The class corresponding to the listener object will contain methods from the listener interface that handle the event.

The listener interface that is used depends on the type of event the application wishes to capture. For example, the event of a user clicking on a button would be handled by the ActionListener, a keystroke by the KeyListener, a mouse movement by the MouseListener interfaces, and so on. The methods declared in an interface must have their method bodies written in the application program. For ActionListener, there is only one corresponding method,
actionPerformed, so this method would perform any actions resulting from the event.On the other hand, the KeyListener interface has three corresponding methods:
keyPressed, keyReleased, and keyTyped, so all these would need to be presenting the application code.

To abbreviate event-handling class names, the application should include the statement

import java.awt.event.*;

The class declaration must list any listeners that the class implements. For example, suppose we have a class, CustomerDetails, which is a frame that implements ActionListener and ItemListener. The class declaration would then be

public class CustomerDetails extends JFrame
implements ActionListener, ItemListener
{


The next step is to register the class instance to act as a listener for one or more specific components. For example, the following statement registers the CustomerDetails object to act as an ActionListener for the tradeButton button:

tradeButton.addItemListener(this);

The argument, this, indicates the object corresponding to the current class, namely, CustomerDetails. Each button object for which we wish to handle a corresponding event will have a corresponding listener registration statement. Each listener interface has a corresponding ''add" method for registering a listener object.

The final step is to write the listener method bodies themselves. The ActionListener interface has one corresponding method, actionPerformed. ItemListener also has just one corresponding method, itemStateChanged.

At this stage, you should note that a number of events can invoke the same listener. For example, a user clicking on a button or pressing return after entering text in a text field can generate an ActionListener event in both cases. Consequently, the code in actionPerformed needs to distinguish between button and text field events. Even if only one component type, button, say, is present in a frame or applet, the code would need to distinguish between the different button components.

No comments:

Post a Comment