We help IT Professionals succeed at work.

Best Practice for using Window Listeners in Java

Suppai
Suppai asked
on
Medium Priority
213 Views
Last Modified: 2013-11-23
I have learned that a good way to use a windowlistener for e.g. applying another window close action to a JFrame than usual is as shown in the code snippet. Is it the best way? If I want to refer to the JFrame to which Im adding it, how do I do it, as you can see my code snippet doesnt work as it is meant to.
this.addWindowListener(new WindowAdapter() {
					public void windowClosing(WindowEvent e) {
						if (JOptionPane.showConfirmDialog(this,
								"Do you want to save the definition lists before exiting " +
								"the application?",	"Save Settings?",
								JOptionPane.YES_NO_OPTION)==JOptionPane.YES_OPTION) {							
							FileHandler.writeSettings(getCurrentSettings());
						}
						this.dispose();
						System.exit(0);
					}
				}
		)

Open in new window

Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2016

Commented:
It's actually better not to use anonymous classes as they are problematic. A named private inner class would be better, but if you are going to do it like that then:

            this.addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent e) {
                    if (JOptionPane.showConfirmDialog(null,
                            "Do you want to save the definition lists before exiting " +
                            "the application?",     "Save Settings?",
                            JOptionPane.YES_NO_OPTION)==JOptionPane.YES_OPTION) { 
                        //FileHandler.writeSettings(getCurrentSettings());
                            }
                    NameOfFrameClass.this.dispose();
                    System.exit(0);
                }
            });

Open in new window

Author

Commented:
A named private inner class? How would I do that? As a nested class that extends WindowAdapter or how would I do it?
CERTIFIED EXPERT
Top Expert 2016
Commented:
>>As a nested class that extends WindowAdapte

Exactly

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010
Commented:
your code should be:

this.addWindowListener(new WindowAdapter() {
                                        public void windowClosing(WindowEvent e) {
                                                if (JOptionPane.showConfirmDialog(this,
                                                                "Do you want to save the definition lists before exiting " +
                                                                "the application?",     "Save Settings?",
                                                                JOptionPane.YES_NO_OPTION)==JOptionPane.YES_OPTION) {                                                  
                                                        FileHandler.writeSettings(getCurrentSettings());
                                                }
                                                event.getWindow().dispose();
                                                System.exit(0);
                                        }
                                }
                )
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.