Grateful if someone can help me on this. I have written a Java program to pop up a dialog box. I cannot figure out how to handle the window closing event (pressing the X button on the top right corner). I read a Swing book, and it said that the function SetDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE) replaces the addWindowListener anonymous inner class for handling the closing event, but still it does not work. I have appended the java file: import javax.swing.*; public class DialogWindow { private JFrame parent; private JOptionPane optionPane; private JDialog dialog; public DialogWindow(JFrame parent) { this.parent = parent; initGUI(); initEventControl(); } public void initGUI() { optionPane = new JOptionPane("Confirm booking?", JOptionPane.QUESTION_MESSAGE, JOptionPane.YES_NO_OPTION); dialog = optionPane.createDialog(parent, "Booking Confirmation"); dialog.setVisible(true); } public void initEventControl() { dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); switch (((Integer)optionPane.getValue()).intValue()) { case JOptionPane.YES_OPTION: System.out.println("I have selected yes"); break; case JOptionPane.NO_OPTION: System.out.println("I have selected no"); break; } } }
Manfred Leonhardt
Ranch Hand
Joined: Jan 09, 2001
Posts: 1492
posted
0
Hi Rudy, Stick with what works. Tried and true AWT events! Before your call the setVisible method of dialog, do the following:
This will work for sure no matter what any book says! Regards, Manfred.
Rudy Yeung
Ranch Hand
Joined: Dec 27, 2000
Posts: 183
posted
0
Despite adding your code, I still have difficulty enclosing the dialog window, and I still do not know the cause. There are a whole bunch of error messages complaining about the unknown source shown on the DOS shell upon closing the window like the following: at javax.swing.AbstractButton.fireActionPerformed (Unknown Source) at javax.swing.AbstractButton$ForwardActionEvents.actionPerformed (Unknown Source) at javax.swing.DefaultButtonModel.fireActionPerformed (Unknown Source) ... at java.awt.Component.processMouseEvent(Unknown Source) ...
Manfred Leonhardt
Ranch Hand
Joined: Jan 09, 2001
Posts: 1492
posted
0
Hi Rudy, I am running JDK1.2 under Win98SE and with just a little change to your code it seems to work good. I am not sure what you are trying to do, but here is the slight change I made to handle the x (close) window button:
The only problems that I encountered when user hits x was that the optionPane.getValue() was null and I got a NullPointerException. Once I placed the check in I got no errors. P.S. Forget the stuff I said about WindowListener ... your stuff works just as well. Manfred. [This message has been edited by Manfred Leonhardt (edited February 04, 2001).]
Rudy Yeung
Ranch Hand
Joined: Dec 27, 2000
Posts: 183
posted
0
Manfred, After changing the code a little bit according to your given snippet, my dialog window can now close properly. I do not realize that (Integer)optionPane.getValue() will return null if I choose the window closing option. Now, I learn one more thing. I really appreciate your help. Thank you. Rudy
Rudy Yeung
Ranch Hand
Joined: Dec 27, 2000
Posts: 183
posted
0
Manfred, BTW, do you know if there is any way I can pipe any runtime error message to a file? For the previous example, I think the Nullpointer exception message is at the very beginning, but I miss it because the DOS shell does not have a scroll bar to scroll up.