[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Best Practice for using Window Listeners in Java

Posted on 2009-02-18
4
Medium Priority
?
189 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

0
Comment
Question by:Suppai
  • 2
4 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 23673589
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

0
 

Author Comment

by:Suppai
ID: 23674003
A named private inner class? How would I do that? As a nested class that extends WindowAdapter or how would I do it?
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 900 total points
ID: 23674029
>>As a nested class that extends WindowAdapte

Exactly
0
 
LVL 92

Assisted Solution

by:objects
objects earned 600 total points
ID: 23674936
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);
                                        }
                                }
                )
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month20 days, 5 hours left to enroll

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question