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!