Event Handling

An event is an object that’s generated when the user does something noteworthy with one of your user interface components. Then this event object is passed to a special method you create, called an event listener. The event listener can examine the event object, determine exactly what type of event occurred, and respond accordingly. If the user clicks a button, the event listener might write any data entered by the user via text fields to a file. If the user passes the mouse over a label, the event handler might change the text displayed by the label. And if the user selects an item from a combo box, the event handler might use the value that was selected to look up information in a database.

Java provides several types of event objects, represented by various classes that all inherit AWTEvent. The most commonly used event objects are ActionEvent and ItemEvent. Both of these event objects are described in separate entries in this part.

Each event object has a corresponding listener interface that you use to create an object that can listen for the event and handle it when it is generated. The most common listener interfaces are ActionListener and ItemListener. Both of these interfaces are covered in separate entries in this part.

To write Java code that responds to events, you have to do the following:

1. Create a component that can generate events.

Add buttons or other components that generate events to your frame so that it displays components the user can interact with.

To make it easy to refer to the components that generate events, declare the variables that refer to them as private class fields, outside the constructor for the frame or any other method. For example:

private JButton button1;

Then, in the constructor for the frame class, you can create the button. Here’s code that creates a panel, creates a button, adds the button to a panel, and then adds the panel to the frame:

JPanel panel = new JPanel();

button1 = new JButton(“Click me!”);

panel.add(button1);

this.add(panel);

Note that this code appears in the constructor of the frame class, so in the last line, this refers to the frame.

2. Create a class that implements the listener interface for the event you want to handle.

To handle action events, for example, you should create a class that implements the ActionListener interface. The easiest way is to create an inner class within your frame class. That way, the inner class can reference the component variables that you declared as class variables in Step 1. Here’s an example of an inner class header for a listener that will listen for action events:

private class ClickListener

implements ActionListener

3. Write the code for any methods defined by the listener.

When you implement a listener interface, you must provide an implementation of each method defined by the interface. Most listener interfaces define just one method, corresponding to the type of event the interface listens for. The ActionListener interface, for example, defines a method named actionPerformed, which is called whenever an action event is created. Thus, the code you place inside the actionPerformed method is executed when an action event occurs.

Here’s an actionPerformed method that responds to action events:

public void actionPerformed(ActionEvent e)

{

if (e.getSource() == button1)

button1.setText(“You clicked!”);

}

This code changes the text displayed by button1 if the event source is button1.

4. Register the listener with the source.

The final step is registering the event listener with the event source. Every component that serves as an event source provides a method that lets you register event listeners to listen for the event. For ActionEvent sources, the method is addActionListener. Here’s a modification to the frame constructor code that creates the button1 button and registers the frame class as the action event listener:

button1 = new JButton(“Click me!”);

button1.addActionListener(

new ClickListener());

Here, an instance of the inner ClickListener class is created and registered as the ActionListener for the button.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset