• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Prompting the user whether to close the JFrame

 
Ranch Hand
Posts: 664
Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

scr.GIF
[Thumbnail for scr.GIF]
screenshot
 
Sheriff
Posts: 22849
132
Eclipse IDE Spring Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:
 
Jon Camilleri
Ranch Hand
Posts: 664
Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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:
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Jon Camilleri
Ranch Hand
Posts: 664
Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 664
Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 664
Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 22849
132
Eclipse IDE Spring Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic