aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Prompting the user whether to close the JFrame Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Prompting the user whether to close the JFrame" Watch "Prompting the user whether to close the JFrame" New topic
Author

Prompting the user whether to close the JFrame

Jon Camilleri
Ranch Hand

Joined: Apr 25, 2008
Posts: 659

I would like to know how to add an event that prompts the user whether I can add an event handler for asking the user to confirm whether the program will exit when they select the [x] icon.




[Thumbnail for scr.GIF]



Jon
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19556
    
  16

Set the default close operation to JFrame.DO_NOTHING, then use a WindowListener and listen for the windowClosing event. To close the frame now all you need to do is call dispose() on the frame. So:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Jon Camilleri
Ranch Hand

Joined: Apr 25, 2008
Posts: 659

Rob Spoor wrote:Set the default close operation to JFrame.DO_NOTHING, then use a WindowListener and listen for the windowClosing event. To close the frame now all you need to do is call dispose() on the frame. So:


It's a pretty good hint, however I got a few questions:

1. For the sake of consistency I would like to declare all menuItems within MyFrame as private variables, but that would render my UI rendering quite
complex and hardcoded. I guess I'd rather have classes which generate menu items. Do you think that making menu items more readable by declaring them as
private members of JFrame is better programming practice, or else should I sub-class each and every menu item (which sounds too gory)?
2. I think I got a few compilation errors and my listeners incorrect however, this article will probably be helpful. Any hints?




Compilation errors...
C:\Documents and Settings\Jon\workspace\JavaDev\myCode Snippets\ChooserTest.java:22: cannot find symbol
symbol : variable DO_NOTHING
location: class javax.swing.JFrame
frm.setDefaultCloseOperation(JFrame.DO_NOTHING);
^
C:\Documents and Settings\Jon\workspace\JavaDev\myCode Snippets\ChooserTest.java:119: windowDeactivated(java.awt.event.WindowEvent) in MyFrame cannot implement windowDeactivated(java.awt.event.WindowEvent) in java.awt.event.WindowListener; attempting to assign weaker access privileges; was public
private void windowDeactivated(WindowEvent e) {}
^
C:\Documents and Settings\Jon\workspace\JavaDev\myCode Snippets\ChooserTest.java:107: windowActivated(java.awt.event.WindowEvent) in MyFrame cannot implement windowActivated(java.awt.event.WindowEvent) in java.awt.event.WindowListener; attempting to assign weaker access privileges; was public
private void windowActivated(WindowEvent e) {}
^
C:\Documents and Settings\Jon\workspace\JavaDev\myCode Snippets\ChooserTest.java:120: windowDeiconified(java.awt.event.WindowEvent) in MyFrame cannot implement windowDeiconified(java.awt.event.WindowEvent) in java.awt.event.WindowListener; attempting to assign weaker access privileges; was public
private void windowDeiconified(WindowEvent e) {}
^
C:\Documents and Settings\Jon\workspace\JavaDev\myCode Snippets\ChooserTest.java:121: windowIconified(java.awt.event.WindowEvent) in MyFrame cannot implement windowIconified(java.awt.event.WindowEvent) in java.awt.event.WindowListener; attempting to assign weaker access privileges; was public
private void windowIconified(WindowEvent e) {}
^
C:\Documents and Settings\Jon\workspace\JavaDev\myCode Snippets\ChooserTest.java:118: windowClosed(java.awt.event.WindowEvent) in MyFrame cannot implement windowClosed(java.awt.event.WindowEvent) in java.awt.event.WindowListener; attempting to assign weaker access privileges; was public
private void windowClosed(WindowEvent e) {}
^
C:\Documents and Settings\Jon\workspace\JavaDev\myCode Snippets\ChooserTest.java:108: windowClosing(java.awt.event.WindowEvent) in MyFrame cannot implement windowClosing(java.awt.event.WindowEvent) in java.awt.event.WindowListener; attempting to assign weaker access privileges; was public
private void windowClosing(WindowEvent e)
^
C:\Documents and Settings\Jon\workspace\JavaDev\myCode Snippets\ChooserTest.java:122: windowOpened(java.awt.event.WindowEvent) in MyFrame cannot implement windowOpened(java.awt.event.WindowEvent) in java.awt.event.WindowListener; attempting to assign weaker access privileges; was public
private void windowOpened(WindowEvent e) {}
^
C:\Documents and Settings\Jon\workspace\JavaDev\myCode Snippets\ChooserTest.java:40: cannot find symbol
symbol : variable panel
location: class MyFrame
panel = new JPanel(new BorderLayout());
^
C:\Documents and Settings\Jon\workspace\JavaDev\myCode Snippets\ChooserTest.java:41: cannot find symbol
symbol : variable panel
location: class MyFrame
add(panel, BorderLayout.CENTER);
^
C:\Documents and Settings\Jon\workspace\JavaDev\myCode Snippets\ChooserTest.java:46: cannot find symbol
symbol : variable filemenu
location: class MyFrame
filemenu = new JMenu("File");
^
C:\Documents and Settings\Jon\workspace\JavaDev\myCode Snippets\ChooserTest.java:47: cannot find symbol
symbol : variable filemenu
location: class MyFrame
menubar.add(filemenu);
^
C:\Documents and Settings\Jon\workspace\JavaDev\myCode Snippets\ChooserTest.java:50: cannot find symbol
symbol : variable Keystroke
location: class MyFrame
openmenu.setAccelerator(Keystroke.getKeyStroke("ctrl O"));
^
C:\Documents and Settings\Jon\workspace\JavaDev\myCode Snippets\ChooserTest.java:51: cannot find symbol
symbol : variable filemenu
location: class MyFrame
filemenu.add(openmenu);
^
C:\Documents and Settings\Jon\workspace\JavaDev\myCode Snippets\ChooserTest.java:54: cannot find symbol
symbol : variable textarea
location: class MyFrame
textarea = new TextArea();
^
C:\Documents and Settings\Jon\workspace\JavaDev\myCode Snippets\ChooserTest.java:55: cannot find symbol
symbol : variable textarea
location: class MyFrame
textarea.setColumns(20);
^
C:\Documents and Settings\Jon\workspace\JavaDev\myCode Snippets\ChooserTest.java:56: cannot find symbol
symbol : variable textarea
location: class MyFrame
textarea.setRows(10);
^
C:\Documents and Settings\Jon\workspace\JavaDev\myCode Snippets\ChooserTest.java:57: cannot find symbol
symbol : variable textarea
location: class MyFrame
panel.add(textarea);
^
C:\Documents and Settings\Jon\workspace\JavaDev\myCode Snippets\ChooserTest.java:57: cannot find symbol
symbol : variable panel
location: class MyFrame
panel.add(textarea);
^
C:\Documents and Settings\Jon\workspace\JavaDev\myCode Snippets\ChooserTest.java:70: cannot find symbol
symbol: variable textarea
textarea.setText(readTextFile(chooser.getSelectedFile()));
^
20 errors

Tool completed with exit code 1


So having reverted to version 1 , I'm wondering whether there is a workaround to enable inner classes to access the textarea object



The complete code for version 1 follows:
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13884
    
  10

It should be DO_NOTHING_ON_CLOSE instead of DO_NOTHING.

Also, you cannot make methods like windowDeactivated, windowActivated, windowDeiconified etc. private. They are declared as public in the interfaces that they come from, so they must be public in your class.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Jon Camilleri
Ranch Hand

Joined: Apr 25, 2008
Posts: 659

Jesper de Jong wrote:It should be DO_NOTHING_ON_CLOSE instead of DO_NOTHING.

Also, you cannot make methods like windowDeactivated, windowActivated, windowDeiconified etc. private. They are declared as public in the interfaces that they come from, so they must be public in your class.


Oh ok, what about my other q's?
Jon Camilleri
Ranch Hand

Joined: Apr 25, 2008
Posts: 659

Jesper de Jong wrote:It should be DO_NOTHING_ON_CLOSE instead of DO_NOTHING.

Also, you cannot make methods like windowDeactivated, windowActivated, windowDeiconified etc. private. They are declared as public in the interfaces that they come from, so they must be public in your class.


Thanks, how about my other questions (hoping someone will read them )

So far, I have a frame which won't close, what is wrong with my listening code?


Related links:
1. How to write Window Listeners (Oracle)
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13884
    
  10

Move the code that you put in the windowClosed method to the windowClosing method.

As you can read in the documentation of interface WindowListener, windowClosed is called after dispose has been called on the window, and windowClosing is...
java.awt.event.WindowListener wrote:
Invoked when the user attempts to close the window from the window's system menu.
Jon Camilleri
Ranch Hand

Joined: Apr 25, 2008
Posts: 659

Jesper de Jong wrote:Move the code that you put in the windowClosed method to the windowClosing method.

As you can read in the documentation of interface WindowListener, windowClosed is called after dispose has been called on the window, and windowClosing is...
java.awt.event.WindowListener wrote:
Invoked when the user attempts to close the window from the window's system menu.


Sorry, I should have seen that coming, now it works as expected

As a general design rule, do you suggest declaring the components (e.g. menuitems, textarea) within the JFrame as private variables, so that the code is more readable? This is because I had to declare TextArea as a private variable, so that the inner class within MyFrame would be able to access textarea.
Is there some nifty workaround to access variables of outer classes?

I think I need to research the implementation of the Keystroke as well to enable modfiers (such as Ctrl + O to access the openmenu), because Keystroke does not seem to document KEY_TYPED events.



Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19556
    
  16

Jesper de Jong wrote:It should be DO_NOTHING_ON_CLOSE instead of DO_NOTHING.

Right. My bad, I didn't look it up. All the closing constants end with _ON_CLOSE.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Prompting the user whether to close the JFrame
 
Similar Threads
JScrollBar, key events
How to set the text in JTextarea from bottom.
Adding JPanel to JFrame
FileFilter for excluding all files (*.*)
Assigning a JPanel to a new JPanel obejct!