Using Interface Callbacks for GUI Event Handlers

So far we've discussed examples of the Comparable and Cloneable interfaces. These interfaces solve compile time issues of forcing a class to have certain behavior. There is an additional way that an interface can be used, to obtain more dynamic behavior. It forms the basis of GUI programming in Java. We have already seen most of this; the only piece that's new is the way that the object-which-implements-an-interface tells the servicing class about itself.

In our earlier example, we called a service method and passed an instance of our handler object as an argument:

Depot.service( myPlane );

Inside the body of service, the method myPlane.refuel() was immediately called back, and that really did the servicing. The piece that is new for GUI callbacks is this. We don't pass our handler object to the service routine each time a service is needed. Instead, we pass the handler object in a one-time registration with the service. The service saves the pointer to our handler. From then on, whenever an event happens, the service looks at its list of saved references. It will call back to every handler object that has registered with it. You will write the handler object so that it does whatever needs to happen when that part of the GUI is tweaked. If it is a handler for a button marked “quit”, the handler will quit the program. If it is a handler for a file open dialog, it will open the file.

This one time registration of the callback target is not such a big change. But it allows the run-time library to take the initiative on deciding when a call back to an event handler must occur, rather than the handler deciding that. Let's look at this in terms of code.

  1. The run-time library defines an interface that promises a method called itHappened() like this:

    interface ActionListener {
         public void itHappened();
    }

    The run-time library makes calls to the method promised by the interface.Your code implements the interface.

  2. In your application code, provide a class that implements the interface:

    class MyCode implements ActionListener {
         public void itHappened() {
              System.out.println("All your base are belong to us");
         }
         // other code
    }

    This is your event handler.

  3. In your application code, make a one-time call to the run-time library to register your interest in button push, mouse click, or other events, and tell it your event handler code.

    MyCode myobj = new MyCode()
    runtime.registerActionListener( myobj );  // it saves a copy of myobj

  4. In the run-time library, the registration routine saves a reference to any objects that register with (i.e., call) it :

    private ActionListener[] registeredObjects; /* init code omitted */
    registerActionListener( ActionListener myobj ) {
               // it saves a copy of myobj
               registeredObjects[i] = myobj;
         }

  5. This is the step all previous steps have been leading up to. Whenever the run-time library notices a GUI event has occurred, it will call back any itHappened() methods that have registered for this event:

    for (ActionListener a : registeredObjects)
              a.itHappened();

    There may be a list of ActionListeners that have registered with the run-time to be informed when that event happens. Often there is just one.

This is how event handling works in the GUI. The whole point is that step 5 runs in a separate thread to your code, and so can call back to your ActionListener at any time. This is therefore known as a callback. That's the end of interfaces. It's time to look at another class from the run-time library.

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

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