User "X" button to close a dialog

Hi, all

I have created a dialog. It will be opened when I click on an applet. But when I click on the "X" button on the upper right corner, it can't be closed. How can I make it works? Thanks.
CodeLoverAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
yongsingConnect With a Mentor Commented:
You need to add a window listener to it:

this.addWindowListener(
  new WindowAdapter() {
    public void windowClosing(WindowEvent e) {
      setVisible(false);
      dispose();
    }
  }
);

"this" is the dialog instance.
0
 
girionisCommented:
 Can you post your code?
0
 
haydes007Commented:
Why not just use a close button with an ActionListener added to it? This way all that needs to be in the ActionListener method is this.setVisable(false);. If you do it the other way, there are like 6(not sure exactly) methods that have to be written, even if you dont use them all, for your code to properly compile and work the way you want it to.
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
girionisCommented:
 If it is a JDialoh you used you should'nt need to add a window listener in order to close it. The following would do just as fine:

public static void main(String [] arguments)
     {
          JDialog myDialog = new JDialog((JFrame)null, true);
          myDialog.show();
          myDialog.dispose();
     }
0
 
yongsingCommented:
If it's an AWT Dialog, I think you need the window listener to close it.
0
 
CodeLoverAuthor Commented:
Thanks, all

yongsing, when I try to put the window listener in the constructor or somewhere else, the dialog can't be opened. Could you please give me some sample codes for it? Thanks.
0
 
yongsingCommented:
You mean the dialog can't be opened after you put the window listener to it? You should show us your code.
0
 
CodeLoverAuthor Commented:
Thanks, all

It works now. :)
0
All Courses

From novice to tech pro — start learning today.