Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

JDialog Dispose and WindowClosing

843804Sep 18 2004 — edited Apr 19 2005
Hi all. I'm trying to get my JDialog to correctly close when it is disposed, but with no luck. Here's a sample code showing the problem. The application will not correctly end because the JDialog is still around.
public class TestJDialog {

	public TestJDialog() {
		super();
	}

	public static void main(String[] args) {
		JDialog d = new JDialog((Frame) null, "Hello");
		d.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		d.addWindowListener(new JDialogWindowAdapter(d));

		d.show();

		//This line will hang your system.
		//Should end the program but doesn't.  This only way
		//this program will end is if you remove the bottom
		//line and click on the x in the dialog.
		d.dispose();

	}
}

class JDialogWindowAdapter extends WindowAdapter {

	private JDialog m_dialog = null;

	/**
	 * Constructs the adapter.
	 * @param d the dialog to listen to.
	 */
	public JDialogWindowAdapter(JDialog d) {
		m_dialog = d;
	}

	public void windowClosing(WindowEvent e) {
		super.windowClosing(e);
		//Dispose the hidden parent so that there are
		//no more references to the dialog and it can 
		//be correctly garbage collected.
		 ((Window) m_dialog.getParent()).dispose();
	}
}
I've looked through the forums, but a lot of people say use EXIT_ON_CLOSE, but that's not possible with my program. I just need to find a way to correctly fire the window closing event using dispose.

Thanks!
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 17 2005
Added on Sep 18 2004
11 comments
3,743 views