Results 1 to 2 of 2
  1. #1
    fam2315 is offline Member
    Join Date
    Feb 2011
    Posts
    78
    Rep Power
    0

    Default JDialog close option

    Is there 2 different listeners for listening for the event when a user actually clicks the "X" in the top right of the jdialog, and when I call dispose().

    I am seeing the WindowListener event being called both when I click X, or when i call dispose() on the frame.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    But which WindowListener event is called for each action?

    To test this, I like to create an SSCCE (as per our previous discussions):

    Java Code:
    import java.awt.Window;
    import java.awt.event.*;
    import javax.swing.*;
    
    @SuppressWarnings("serial")
    public class DialogClose {
       private static JDialog dialog;
       
       private static void createAndShowGui() {
          
          JButton showDialogBtn = new JButton(new AbstractAction("Show Dialog") {
             
             @Override
             public void actionPerformed(ActionEvent arg0) {
                if (dialog == null) {
                    dialog = new JDialog();
                   dialog.addWindowListener(new WindowAdapter() {
                      public void windowClosing(WindowEvent e) {
                         System.out.println("window closing");
                      }
                      
                      public void windowClosed(WindowEvent e) {
                         System.out.println("window closed");
                      }
                   });
                   dialog.add(new JButton(new AbstractAction("close dialog") {
                      public void actionPerformed(ActionEvent e) {
                         JButton source = (JButton)e.getSource();
                         Window win = SwingUtilities.getWindowAncestor(source);
                         win.dispose();
                      }
                   }));
                   dialog.pack();
                   dialog.setLocationRelativeTo(null);
                }
    
                dialog.setVisible(true);
             }
          });
          
          JFrame frame = new JFrame("DialogClose");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.getContentPane().add(showDialogBtn);
          frame.pack();
          frame.setLocationRelativeTo(null);
          frame.setVisible(true);
       }
    
       public static void main(String[] args) {
          SwingUtilities.invokeLater(new Runnable() {
             public void run() {
                createAndShowGui();
             }
          });
       }
    }
    Also the WindowListener API has something to say on this subject as well. If you've not had a chance to look at it, you should give it a gaze.
    Last edited by Fubarable; 08-03-2011 at 06:45 PM.

Similar Threads

  1. Replies: 13
    Last Post: 04-09-2010, 11:15 AM
  2. JAX-WS\METRO - apt with option option key="r"
    By Talkinjive in forum Web Frameworks
    Replies: 0
    Last Post: 01-31-2010, 04:15 PM
  3. Close JDialog
    By sky in forum AWT / Swing
    Replies: 7
    Last Post: 11-16-2009, 06:01 PM
  4. Close a JDialog Programmatically
    By Marcus in forum Advanced Java
    Replies: 1
    Last Post: 07-06-2007, 04:06 PM
  5. Replies: 1
    Last Post: 04-09-2007, 12:05 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •