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: