Article View: borland.public.jbuilder.java.swing
Article #8756Destroying modal JDialog when clicked "OK", "Sys menu's Close", "title bar's X icon"
From: X_Aki Nieminen_X
Date: Wed, 19 Jul 2000 00:00
Date: Wed, 19 Jul 2000 00:00
109 lines
3963 bytes
3963 bytes
system: JBuilder 3.5Pro/Win98/128MB
How to control window events, so that my handlers are always called in every
situation?
I've created a JDialog class, which is opened as modal window from the main
application. Now, I have to do some cleanup when destroying (=disposing) the
dialog instance. There are several methods to destroy window.
1) Click window title bar's "X" icon at the top-right corner
2) Select "Close" from the system menu at the top-left corner
3) Click OK button to close the window
I have to do cleanup code in two phase, first when closing the window and
second when window is actually closed. I've put cleanup code to
"this_windowClosing(WindowEvent e)" and "this_windowClosed(WindowEvent e)"
event handlers.
1st and 2nd closing methods work fine and both event handlers are called by
Java system.
3rd method was getting some problems (OK clicked button handler):
If I call "this.dispose()" inside button handler, then "windowClosing" event
handler is never called, but only "windowClosed" one.
Finally, I created explicitly a new WindowEvent and dispatch it to dialog
instance. See my code below with dispatching code.
Is it legal to do it this way in Java to handle window close operation, can
I dispatch window message without any side effects?
Is there some better and efficient way to this?
==============
[Main application's menuitem handler]
void mnuitmAboutDialog_actionPerformed(ActionEvent e) {
JDialog dialog = new DialogAbout(this, "", true);
dialog.setVisible(true);
}
==============
[My JDialog class]
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class DialogAbout extends JDialog {
JPanel panel1 = new JPanel();
BorderLayout borderLayout1 = new BorderLayout();
JLabel jLabel1 = new JLabel();
JButton btnOk = new JButton();
public DialogAbout(Frame frame, String title, boolean modal) {
super(frame, title, modal);
try {
jbInit();
pack();
}
catch(Exception ex) {
ex.printStackTrace();
}
}
public DialogAbout() {
this(null, "", false);
}
void jbInit() throws Exception {
this.setTitle("Dialog Title");
this.addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(WindowEvent e) {
this_windowClosing(e);
}
public void windowClosed(WindowEvent e) {
this_windowClosed(e);
}
});
panel1.setLayout(borderLayout1);
btnOk.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
btnOk_actionPerformed(e);
}
});
getContentPane().add(panel1);
jLabel1.setText("Click OK to close the window");
btnOk.setText("OK");
panel1.add(jLabel1, BorderLayout.NORTH);
panel1.add(btnOk, BorderLayout.SOUTH);
}
void btnOk_actionPerformed(ActionEvent e) {
this.dispatchEvent(new WindowEvent(this, WindowEvent.WINDOW_CLOSING));
// this.dispose(); // this will skip windowClosing event !!!!
}
void this_windowClosing(WindowEvent e) {
JOptionPane.showMessageDialog(null, "closing");
// do some cleanup code here before disposing the dialog instance
this.dispose();
}
void this_windowClosed(WindowEvent e) {
// do some cleanup code here after disposing the dialog instance
JOptionPane.showMessageDialog(null, "closed");
}
}
Technical Details
Message-ID:
<397576c9@dnews>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 111d45,1dadf17715fc8d0f,start X-Google-Attributes: gid111d45,public From: "X_Aki Nieminen_X" <no@email.abc> Subject: Destroying modal JDialog when clicked "OK", "Sys menu's Close", "title bar's X icon" Date: 2000/07/19 Message-ID: <397576c9@dnews> X-Deja-AN: 648135735 X-Trace: 19 Jul 2000 02:37:13 -0800, 194.142.156.86 Newsgroups: borland.public.jbuilder.java.swing X-MimeOLE: Produced By Microsoft MimeOLE V4.72.3110.3