[4689] in java-interest
Re: Problem with handling events (killing windows in a menubar of a frame & WINDOW_DESTROY)
daemon@ATHENA.MIT.EDU (James Knighten (Am One!))
Wed Jan 10 22:32:16 1996
Date: Thu, 11 Jan 1996 11:09:20 +0900
From: "James Knighten (Am One!)" <knightjm@pms.mmlab.toshiba.co.jp>
To: Sian Tan <tan@cdr.stanford.edu>
Cc: java-interest@java.sun.com
Sian Tan wrote:
>
> Hi.
>
> 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;
> }
> }
> --
>
> **************************
> Sian Tan
> Center for Design Research
> 560 Panama Street
> Stanford, CA 94305
> Tel: 1-415-725 0161
> Fax: 1-415-725 8475
> **************************
> -
Hi Sian Tan,
I think a couple of other people answered the question about how to
fix your problem. By switching to super.handleEvent(evt) in your switch
default statement. I'd like to add a little something myself. I was
just perusing the tutorial and I ran across a paragraph about the
System.exit() method. It said that it should be used cautiously
in applets because exit() shuts down the java interpreter not just the
applet. I've also noticed that in many code examples that can run as
either applets or applications there is a distinction made between the
two when an evt.WINDOW_DESTROY occurs. Here's an example...
public boolean handleEvent(Event evt) {
if (evt.id == Event.WINDOW_DESTROY) {
if (inAnApplet) {
dispose();
return true;
} else {
System.exit(0);
}
}
return super.handleEvent(evt);
}
Where the class is a subclass of frame and inAnApplet is a boolean that
is set to false when and if the main() method is called. I wouldn't have
mentioned it, except that this code fragment occurs often in the
tutorial's examples. Maybe this can save you some grief in the future.
jim
PS- the example is in '/ui/overview/example/GUIWindow.java' under your
tutorial directory.
-
This message was sent to the java-interest mailing list
Info: send 'help' to java-interest-request@java.sun.com