Event Propagation 

  • In our button example, we subclassed a button in order to handle its events. However, if this were the only way you could handle events, things would get cumbersome fairly quickly. If we needed to subclass each control that we wanted to handle events for, the number of classes in our code would get large and unwieldy quickly. Happily, this isn't the case. Events aren't just passed to the superclasses of the components where the events occur; they are also passed to the parents of those components. 
  • For example, if our button was placed in a dialog, we could also handle the events in the dialog's handleEvent() method. 
  • class MyDialog extends Dialog
    {
    private Button _okButton;
    
    public void MyDialog()
        {
       ... initialization code...
        _okButton = new Button("OK");
        Panel panel = new Panel();
        panel.add(_okButton);
        add(panel);
        ... more initialization code...
        }
    
    public boolean action(Event event, Object arg)
        {
        if (event.target == _okButton)
            System.out.println("The OK button was pressed");
        return super.action(event, arg);
        }
    }
  • Notice that in this case, the Dialog would need to make sure that the button generated the event before handling it. To do so, the dialog's action() method utilizes information contained in the event object. Let's take a closer look at the event object. 

Additional Resources:

Other Methods for Handling Events
Table of Contents
Event Objects 


Graphics & Media Lab. >> Библиотека | Курсы | Графикон

Hosted by Graphics & Media Lab
http://graphics.cs.msu.su
lab_logo
mailto: Laboratory