Chapter 6.4.1 – Event Handling | Introduction to Programming Using Java

Chapter 6.4.1 – Event Handling | Introduction to Programming Using Java

 

6.4.1 Event Handling

 

 

event handling

 

For an event to have any effect, a program must detect the event and react to it. In order to detect an event, the program must “listen” for it. Listening for events is something that is done by an object called an event listener. An event listener object must contain instance methods for handling the events for which it listens. For example, if an object is to serve as a listener for events of type MouseEvent, then it must contain the following method (among several others):

 

9 5 Chapter 6.4.1 - Event Handling | Introduction to Programming Using Java

 

The body of the method defines how the object responds when it is notified that a mouse button has been pressed. The parameter, evt, contains information about the event. This information can be used by the listener object to determine its response.

The methods that are required in a mouse event listener are specified in an interface named MouseListener. To be used as a listener for mouse events, an object must implement this MouseListener interface. Java interfaces were covered in Subsection 5.7.1. (To review briefly: An interface in Java is just a list of instance methods. A class can “implement” an interface by doing two things.

First, the class must be declared to implement the interface, as in “class MyListener implements MouseListener” or “class MyApplet extends JApplet implements MouseListener”. Second, the class must include a definition for each instance method specified in the interface. An interface can be used as the type for a variable or formal parameter. We say that an object implements the MouseListener interface if it belongs to a class that implements the MouseListener interface.

Note that it is not enough for the object to include the specified methods. It must also belong to a class that is specifically declared to implement the interface.)

 

Chapter 6.4.1 - Event Handling | Introduction to Programming Using Java

 

Many events in Java are associated with GUI components. For example, when the user presses a button on the mouse, the associated component is the one that the user clicked on. Before a listener object can “hear” events associated with a given component, the listener object must be registered with the component. If a MouseListener object, mListener, needs to hear mouse events associated with a Component object, comp, the listener must be registered with the component by calling “comp.addMouseListener(mListener);”.

The addMouseListener() method is an instance method in class Component, and so can be used with any GUI component object. In our first few examples, we will listen for events on a JPanel that is being used as a drawing surface.

The event classes, such as MouseEvent, and the listener interfaces, such as MouseListener, are defined in the package java.awt.event. This means that if you want to work with events, you should either include the line “import java.awt.event.*;” at the beginning of your source code file or import the individual classes and interfaces.

Admittedly, there is a large number of details to tend to when you want to use events. To summarize, you must

  1. Put the import specification “import java.awt.event.*;” (or individual imports) at the beginning of your source code;
  2. Declare that some class implements the appropriate listener interface, such as MouseListener;
  3. Provide definitions in that class for the subroutines from the interface;
  4. Register the listener object with the component that will generate the events by calling a method such as addMouseListener() in the component.

Any object can act as an event listener, provided that it implements the appropriate interface. A component can listen for the events that it itself generates. A panel can listen for events from components that are contained in the panel. A special class can be created just for the purpose of defining a listening object. Many people consider it to be good form to use anonymous inner classes to define listening objects (see Subsection 5.7.3). You will see all of these patterns in examples in this textbook.

 

Chapter 6.4.1 - Event Handling | Introduction to Programming Using Java

 

 

 

 

Read More…

Introduction to Programming Using Java – David J. Eck

Chapter 5.5.3 – Example: Vehicles | Introduction to Programming Using Java

Chapter 5.6 – This and Super | Introduction to Programming Using Java

Chapter 5.7 – Interfaces | Introduction to Programming Using Java

Chapter 5.8 – Nested Classes | Introduction to Programming Using Java

Chapter 6 – Introduction to GUI Programming | Introduction to Programming Using Java

1 thought on “Chapter 6.4.1 – Event Handling | Introduction to Programming Using Java”

Leave a Comment