[4726] in java-interest

home help back first fref pref prev next nref lref last post

Re: Problem with handling events (killing windows in a menubar of a frame & WINDOW_DESTROY)

daemon@ATHENA.MIT.EDU (Patrick Taylor)
Thu Jan 11 16:40:06 1996

Date: Thu, 11 Jan 1996 13:05:40 -0500
From: Patrick Taylor <patrick.taylor@template.com>
To: java-interest@java.sun.com, tan@cdr.stanford.edu
In-Reply-To: <199601110212.SAA14577@java.sun.com>


> From: bene@NewZealand.Sun.COM (Ben Evans - SolNet Technologies)
> Date: Thu, 11 Jan 1996 09:17:22 +1300
> Subject: Re: Problem with handling events (killing windows in a menubar of a frame & WINDOW_DESTROY)
> 
> On Wed, 10 Jan 1996, Sian Tan <tan@cdr.stanford.edu> wrote:
> > 
> > I'm trying to spruce up my user interface to have menuitem interaction. 
> > I'm using the method below to take care of mouse actions on the menubar 
> > of the frame. This works fine by itself. It kills the window. However, 
> > when I try to add the second code segment below into the same class, the 
> > window closes if I click the "close window" button on the window, but my 
> > menu items fail to function. What am I doing wrong here?
> > 
> > Reply by e-mail to: tan@cdr.stanford.edu is requested.
> > 
> > *************** 1st code segment ***************
> >  public boolean action(Event evt, Object obj) {
> >         if (evt.target instanceof MenuItem) {
> >             String label = (String)obj;
> >             if (label.equals(QUIT)){
> >                 this.dispose();
> >                 System.exit(0); 
> >             }
> > 
> > 	etc. -------- etc.
> > 
> > *************** 2nd code segment ***************
> >   public boolean handleEvent(Event evt) {
> >         switch(evt.id){
> >             case Event.WINDOW_DESTROY:{
> >                 this.dispose();
> >                 System.exit(0);
> >                 return true;
> >             }
> >             default:
> >                 return false;
> >         }
> >   }
> 
> I think the problem is that the default branch in your handleEvent()
> does not call the action() method. Adding the following code should work...
> 
> *************** New 2nd code segment ***************
>   public boolean handleEvent(Event evt) {
>         switch(evt.id){
>             case Event.WINDOW_DESTROY:{
>                 this.dispose();
>                 System.exit(0);
>                 return true;
>             }
>             default:
>                 return action( evt, evt.arg );  <<--
>         }
>   }
> 
> You probably thought (as I did) that returning false from handleEvent()
> would cause the AWT to call your overrided action() method. Apparently
> this is not so! It looks like you have to manually call your own action()
> method.
> 
> ( Java gurus: am I right about this? Why doesn't AWT automatically call
>   the action() method? )

No.  If handleEvent returns false, the event is passed to the handleEvent
method of the parent COMPONENT.  What you want to do is pass it to the
handleEvent method of the parent CLASS that you've overridden.  Try this
instead:

           default:
              return super.handleEvent(evt);

By passing the event up the superclass hierarchy, it will eventually get to
handleEvent method in Component, which will call your redefined action
method for ACTION_EVENTs, or another event-related method appropriate to
the event type, such as mouseDown.  Your version of handleEvent calls the
action method for all events that aren't WINDOW_DESTROY events, which is
wrong.  Take a good look at the source for the handleEvent and postEvent
methods of the Component class.

It seems to me that it is seldom appropriate to return false from a
redefined handleEvent method, since this circumvents the default processing
of that event.  Any comments from the AWT designers?

__________________________________________________________________
Patrick Taylor                                   Software Engineer
Template Software                                 703/318-1228 vox
13100 Worldgate Drive, Suite 340                  703/318-7378 fax
Herndon, VA 22070-4382 (USA)           patrick.taylor@template.com
-
This message was sent to the java-interest mailing list
Info: send 'help' to java-interest-request@java.sun.com

home help back first fref pref prev next nref lref last post