I wanted to test the idea of handleEvent() passing to the enclosing environment. ... and what does enclosing environment mean?
(no texts at this time)
The given code (in and out of button)always returned :
This might have been a button event:
* so ignored action, MouseDown - expected since return true in handleEvent
When changed all returns to true, still always returned :
This might have been a button event: (still expected no diff)
When changed handleEvent to false, still always returned :
This might have been a button event: (this was not expected - what happened to action? )
* Discussion
When changed handleEvent to the following, worked as expected (reasonable - spefically "handles " Event )
When changed handleEvent to the following, worked only when hit button. Here it would provide Mouse works (x,y) stuff . Made no difference if return true or false. Nothing otherwise
Discussion:
Events and HandleEvent - Core2: page 292, 293 and API
handleEvent:
return true: the event is handled - don't propogate
return false: the event is not handled - propogate to parent in the window hierarchy
return super.handleEvent(evt): the event is not handled - propogate to the parent in the inheritance hierarchy
What is meant by the window hierarchy? generic handleEvent passes to Applet
Remeber that previously I had said that it gives it to the enclosing environment. If a button is instantiated and then added to an applet (or a subclass of applet), then the enclosing environment is applet (or the subclass of applet). The example after this discussion will show how this works.
Conclusion: handleEvent can handle all events in a Container; consider it as a kind of "director"... it is looked at first
How does action() fit in:
public boolean action(Event evt, Object arg) {
... }
(Teach Yourself: pg 255): "Testing for an action by a UI component is a form of event management. UI components produce the special kind of event called an action.
If one uses action(), the method handles only ACTION_EVENTS Nutshell: page 183, discusses Events and Event Types. Note that only Components: Button, Checkbox, Choice, List, MenuItem, and TextField are ACTION_EVENTS
Examples of how to deal with action() in Teach, pg. 255-257
Note: "Although you can handle UI actions in the body of the action() method, it is much more common simply to define a handler method and call that method from action() instead." misleading sentence
This is NOT talking about the handleEvent(). It is talking about making specific methods to handle specific Components.
Core2, pg. 226, "when a button is clicked, the action() method is the one that is triggered."
As we saw in the first example, handleEvent() "overrides" the action(),
so these sentences are true only if there is no handleEvent method there
Nutshell: All Java GUI Components, pg. 108; Handling GUI Events, pg. 114,
Core2, page 292, Teach Yourself, page 233
Almost forgot: when I did do
return super.handleEvent(e);
in the first example, all worked as should. Before the return, I placed
System.out.println("My super is: " + super.getClass().getName());
and got
My super is myButton
Now, example tried with subclassing:
This only does the action of the button. If I change the action return to false, it does both the buttons action and the handleEvent.
In applet, what if there are a lot of buttons; theoretically ask: should Button do the action or Applet?