Hi everyone - I've got a program with a frame as the parent window. The user enters some data into fields and I do a check to see if any of the fields have been left empty. If any have, I want a small dialog box to pop up saying "You forgot to fill 'n' window" .. I created the dialog box (modal) and a window listener so that when the user presses the X on the dialog box, it will close it. However, I CAN'T for the life of me get the "error" dialog box I created to CLOSE!! In the 'window_closing' function I've tried: dispose(); hide(); setVisible(false); Here's my code for the box: // Created outside the function Dialog error = new Dialog(window, "Error", true); public void PopUpError() { error.setSize(200, 200); error.show();
error.addWindowListener( new WindowAdapter() { public void windowClosing( WindowEvent e ) { error.dispose(); } } );
HELP! This is probably easy and I just am not seeing it.
} ------------------ Eric Neuman Sun Certified Programmer for the Java� 2 Platform ------------------
Toshifumi Ishii
Greenhorn
Joined: May 09, 2001
Posts: 3
posted
0
Why not use Swing components!? :-) It seems somehow that AWT Dialog with Modal mode cannot catch WindowClosing event. Could someone tell me why and how(mechanism)? Note: JDialog need not to attach the Listener !! I hope my sample test code can help you. ------------------- /* Dialog Problem : 2001.05.10 Reproduction */ import java.lang.*; import java.io.*; import java.util.*; import java.text.* ; import java.math.* ; import java.applet.* ; import java.awt.* ; import java.awt.event.* ; import javax.swing.* ; public class testpad { /** Main Function */ public static void main(String[] args) { JFrame w = new JFrame("TestPad Frame") ; //Frame w = new Frame("TestPad Frame") ; w.addWindowListener( new WindowAdapter(){ public void windowClosing(WindowEvent e){ System.out.println("Frame Close....") ; System.exit(0) ; } } ) ; w.pack() ; w.setSize( 200,200 ) ; Dimension screen = Toolkit.getDefaultToolkit().getScreenSize() ; w.setLocation( (screen.width - w.getSize().width) / 2 , (screen.height - w.getSize().height) / 2 ) ; w.setVisible(true) ; Frame wf = new Frame() ; // WorkFrame for TmpDialog // (in order to close only TmpDialog) //TmpDialog tp = new TmpDialog(wf,"Error",false) ; // Non-Modal TmpDialog tp = new TmpDialog(wf,"Error",true) ; // Modal //tp.addWindowListener( new WindowAdapter(){ // public void windowClosing(WindowEvent e){ // System.out.println("TmpDialog Close....") ; // ((Frame)((Dialog)e.getSource()).getParent()).dispose() ; // } // } // ) ; } } class TmpDialog extends JDialog { //class TmpDialog extends Dialog { TmpDialog(Frame wf , String title , boolean bMode) { super(wf,title,bMode) ; System.out.println("TmpPanel() Constructor....") ; Dimension screen = Toolkit.getDefaultToolkit().getScreenSize() ; JLabel jl1 = new JLabel("Please wait a while",JLabel.CENTER) ; //Label jl1 = new Label("Please wait a while",Label.CENTER) ; jl1.setOpaque(true) ; jl1.setForeground(Color.white) ; jl1.setBackground(Color.blue) ; this.getContentPane().add(jl1) ; //add(jl1) ; this.pack() ; this.setSize(300,80) ; this.setLocation( (screen.width - this.getSize().width) / 2 , (screen.height - this.getSize().height) / 2 ) ; this.toFront() ; this.setVisible(true) ; } }
--------------------------------------- Without a hurt,the heart is hollow.... I like this English maxim. :-) ---------------------------------------
------------------ I am very happy to find this site.
I am very happy to find this site.
Amit Agarwal
Ranch Hand
Joined: May 09, 2001
Posts: 92
posted
0
Eric, since you just want to show the error message, you can use an inbuilt swing message dialog by using the following: JOpeionPane.showMessageDialog(parent, message, title, DialogType) for example: JOptionPane.showMessageDialog(this, "Please fill all fields!", "Error", JOptionPane.ERROR_MESSAGE);