Summary of Event Handling

We have seen a specific example of handling the event generated by closing a Window. It can be written more compactly if you write it as an inner class or even as an anonymous class. You can junk even more unneeded code if you use an adapter class.

There are several kinds of events for the different controls: a button generates one kind of event, a text field another, and so on. To impose order and to divide them up according to what they do, there are approximately 12 individual Listener interfaces shown in Table 20-1 on page 507. They all work the same way: you write a handler class that implements the interface, and register it with the control. When the control fires an event, the method in the handler object that you registered is called.

The key points about GUI handling include the following:

  • Each SomethingListener interface has one or more methods showing the signature of a method that is called when the corresponding SomethingEvent occurs.

  • Your handler code implements the SomethingListener interface and therefore has methods with signatures that fulfill those promised in the interface.

  • Each control has a method called addSomethingListener(). The addSomethinglistener() method takes a single argument, an object that implements the SomethingListener interface.

  • Swing requires that all code that might affect GUI components be executed from the event-dispatching thread. A section in Chapter 21 explains this concept.

  • You call addSomethingListener(), using an instance of your handler class as the parameter. This registers your object as the handler for that kind of event for that control.

The SomethingEvent class is a subclass of class AWTEvent and stores all the information about what just happened, where, and when. An object of the SomethingEvent class is passed to the method in the SomethingListener interface. It sounds more complicated than it is. Figure 20-4 shows the design pattern.

Figure 20-4. Design pattern of JDK 1.1 event handling

image

You can register several handlers to receive the same single event. You can dynamically (at run-time) remove or add an event handler from a control. You add an event handler with a call shown to the following example:

myComponent.addWindowListener( myEventHandler );

You won't be too surprised to learn that the method to remove one is shown in the following example:

myComponent.removeWindowListener( myEventHandler );

We won't show all of the SomethingEvent classes and SomethingListener interfaces here. You should review them by typing the following:

javap java.awt.event.MouseEvent

Compiled from MouseEvent.java:

public synchronized class java.awt.event.MouseEvent
          extends java.awt.event.InputEvent
{
     public static final int MOUSE_FIRST;
     public static final int MOUSE_LAST;
     public static final int MOUSE_CLICKED;
     public static final int MOUSE_PRESSED;
     public static final int MOUSE_RELEASED;
     public static final int MOUSE_MOVED;
     public static final int MOUSE_ENTERED;
     public static final int MOUSE_EXITED;
     public static final int MOUSE_DRAGGED;
     int x;
     int y;
     int clickCount;
     boolean popupTrigger;
     public int getX();
     public int getY();
     public java.awt.Point getPoint();
     public synchronized void translatePoint(int, int);
     public int getClickCount();
     public boolean isPopupTrigger();
     public java.lang.String paramString();
     // constructor
     public java.awt.event.MouseEvent(java.awt.Component,
          int,long,int,int,int,int,boolean);
}

Similarly, you can check on the interface that is implemented by your handler by typing the following:

javap java.awt.event.MouseListener

Compiled from MouseListener.java:

public interface java.awt.event.MouseListener extends
          java.lang.Object implements java.util.EventListener {
     public void mouseClicked(java.awt.event.MouseEvent);
     public void mousePressed(java.awt.event.MouseEvent);
     public void mouseReleased(java.awt.event.MouseEvent);
     public void mouseEntered(java.awt.event.MouseEvent);
     public void mouseExited(java.awt.event.MouseEvent);
}

We don't need to show the java.awt.event.MouseAdapter class because it has all the same methods, only with empty bodies. You should use the online browser documentation to look at the public fields and methods of all the other Events and Listeners. A full list of the events and listeners is provided in Chapter 21.

There are a lot of new ideas presented by event handling, so don't worry if it doesn't all make sense now. Sleep on it, reread it, try the sample programs, and it will all come together. Understand the event handling before moving on.

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

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