- 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
class MyDialog extends Dialog
private Button _okButton;
public void MyDialog()
... initialization code...
_okButton = new Button("OK");
Panel panel = new 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
Previous Page |