`WindowListener` acting up, perpetual firing

Leannain

New Member
I have an application with an abstract class that extends \[code\]JDialog\[/code\]. The class as an \[code\]abstract void onClose()\[/code\], and, in the class's constructor, the following code is added:\[code\]addWindowListener(new WindowAdapter() { @Override public void windowClosed(WindowEvent e) { onClose(); }}\[/code\]The event is fired when expected, but then a strange thing happens. When a concrete extension of this class has code to create a new \[code\]JDialog\[/code\] in the \[code\]onClose()\[/code\] method, and this \[code\]JDialog\[/code\]'s \[code\]defaultCloseOperation\[/code\] is \[code\]JDialog.DISPOSE_ON_CLOSE\[/code\], the event is fired continuously, until I force quit the operation.I have isolated the code to the following SSCCE:\[code\]// package removed// imports removedpublic class SSCCE extends JDialog { public static void main(String[] args) { SSCCE s = new SSCCE(); s.pack(); s.setVisible(true); } public SSCCE() { setLayout(new GridLayout(1, 0, 0, 0)); JButton btn = new JButton("click me"); btn.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { dispose(); } }); addWindowListener(new WindowAdapter() { @Override public void windowClosed(WindowEvent e) { System.out .println("SSCCE.SSCCE().new WindowAdapter() {...}.windowClosed()"); onClose(); } }); add(btn); } public void onClose() { JDialog dialog = new JDialog(); dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); dialog.setVisible(true); }}\[/code\]Upon clicking the "click me" button, the blank \[code\]JDialog\[/code\] appears and \[code\]SSCCE.SSCCE().new WindowAdapter() {...}.windowClosed()\[/code\] appears in the console window. When I close the blank dialog, it reappears again and the text appears again.Another really interesting thing is that when I change the initialization line from\[code\]JDialog dialog = new JDialog();\[/code\]to\[code\]JDialog dialog = new JDialog() { @Override public synchronized void addWindowListener(WindowListener l) { super.addWindowListener(l); System.out .println("SSCCE.onClose().new JDialog() {...}.addWindowListener()"); } };\[/code\]I get the following output in the console:When clicking the "click me" button:\[code\]SSCCE.SSCCE().new WindowAdapter() {...}.windowClosed()SSCCE.onClose().new JDialog() {...}.addWindowListener()SSCCE.onClose().new JDialog() {...}.addWindowListener()\[/code\]At the first closing of the dialog:\[code\]SSCCE.SSCCE().new WindowAdapter() {...}.windowClosed()SSCCE.onClose().new JDialog() {...}.addWindowListener()SSCCE.onClose().new JDialog() {...}.addWindowListener()SSCCE.onClose().new JDialog() {...}.addWindowListener()SSCCE.onClose().new JDialog() {...}.addWindowListener()\[/code\]At the second closing of the dialog:\[code\]SSCCE.SSCCE().new WindowAdapter() {...}.windowClosed()SSCCE.onClose().new JDialog() {...}.addWindowListener()SSCCE.onClose().new JDialog() {...}.addWindowListener()SSCCE.onClose().new JDialog() {...}.addWindowListener()SSCCE.onClose().new JDialog() {...}.addWindowListener()SSCCE.onClose().new JDialog() {...}.addWindowListener()\[/code\]Each time I close the dialog, \[code\]addWindowListener(WindowListener l)\[/code\] is called an additional time, even though it I am not intentionally calling it.I don't really want any \[code\]WindowListener\[/code\]s to be registered on the dialog, but I think that simply overriding the \[code\]addWindowListener(...)\[/code\] method and not calling \[code\]super.addWindowListener(...)\[/code\] would be too sloppy.I'm running Java 1.6.0_31 on Mac OS X 10.6.8, using Eclipse Indigo (with WindowBuilder, if it matters).Does anyone have any ideas?Thanks!
 
Top