Wrox Programmer Forums
|
Java GUI Discussions specific to programming Java GUI.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Java GUI section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 12th, 2005, 03:58 AM
Authorized User
 
Join Date: Sep 2005
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to vinay.bk
Default WindowListener


Can anyboody guide me on using windowListener

I am having problem in assigning a WindowListener to a frame and panel

any small program

Thanks & Regards
     vinay
__________________
Thanks & Regards
      vinay
 
Old February 1st, 2006, 05:46 AM
Authorized User
 
Join Date: Oct 2005
Posts: 72
Thanks: 0
Thanked 0 Times in 0 Posts
Default

what exactly you want to attain? it's kind'a hard to imagine.. =D

 
Old February 4th, 2006, 06:23 PM
Authorized User
 
Join Date: Jan 2006
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to longjava
Default

Before I show you an example of how to use a WindowListener, I like to mention couple important things.

1. Only the JFrame and JDialog classes can be used for registering WindowListener, but not the JPanel class.

2. Most of the time you would want to use the WindowAdaper class to register as a listener. Since the WindowListener interface declares seven methods, so if your class implements the WindowListener interface it must implement those seven methods which most of the time you are not interested all those events. Therefore, the Java class library provides the WindowAdapter class that implements the WindowListener interface, but its implemntation for those seven methods are empty. Therefore, you would need to override the methods that you're intertested by providing your own implementations.

---------------------------

Here is an example of how to intercept an WindowEvent to save data before exiting application when users click on the close button:

class MYFrame extends JFrame
{
   public MYFrame()
   {
      super();

      setDefaultCloseOperation( WindowConstants.DO_NOTHING_ON_CLOSE );
      addWindowListener( new WindowAdapter()
      {
         public void windowClosing( WindowEvent evt )
         {
            saveData();
            dispose();
            exitJVM( 0 );
         }
      });
   }

   private void saveData()
   {
      // save data to disk file etc..
   }

   private void exitJVM( exitCode )
   {
      System.exit( exitCode );
   }

   //......
}

Please checking the syntax, I might be making a mistake with the syntax while I typing it.

Hope this will help.

 
Old February 7th, 2006, 12:24 AM
Authorized User
 
Join Date: Aug 2005
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Default

See some more windowListener related examples at,
http://www.javareference.com/jrexamp....jsp?rootcat=2

Rahul

[email protected]
--------------------------
http://www.javareference.com
 
Old May 8th, 2006, 05:26 AM
Authorized User
 
Join Date: Sep 2005
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to vinay.bk
Default

rahul sapkal
 could u give a simple example of your own i am unable to follow the example there


Thanks & Regards
     vinay









Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.