Results 1 to 8 of 8

Thread: Close JDialog

  1. #1
    sky
    sky is offline Member
    Join Date
    Nov 2009
    Posts
    96
    Rep Power
    0

    Default [SOLVED] Close JDialog and set it in the center of the parent frame

    Hi,

    I have a JDialog with buttons, and I want to close the dialog when I press them. I search in Google, but the code I could find is not working for me. I'm trying this inside the method which displays the dialog:

    Java Code:
    JButton cancelButton = new JButton("Cancel");
            buttonPanel.add(cancelButton);
            cancelButton.setActionCommand(CMD_CANCEL);
            
            cancelButton.addActionListener(new ActionListener() {
                                    public void actionPerformed(ActionEvent e) { 
                                        windowAction(e); 
                                    }
                                });
    and then another method like this:

    Java Code:
    private void windowAction(Object actionCommand) {
            
            boolean closeWindow = false;
            String cmd = null;
                    
            if (actionCommand != null) {
                if (actionCommand instanceof ActionEvent) {
                    cmd = ((ActionEvent)actionCommand).getActionCommand();
                } else {
                    cmd = actionCommand.toString();
                }
            }
            
            if (cmd != null) {
                if (cmd.equals(CMD_CANCEL)) {
                    System.out.println("CANCEL");
                    closeWindow = true;
                } else if (cmd.equals(CMD_OK)) {
                    System.out.println("OK");
                    closeWindow = true;
                } else {
                    System.out.println("Command invoked: " + cmd);
                }
            }
            
            if (closeWindow) {
                setVisible(false);
                dispose();
            }
        } // windowAction()
    but the compiler raises an error because it can not find setVisible or dispose methods. Thanks in advance
    Last edited by sky; 11-16-2009 at 06:04 PM.

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

    Default

    setVisible and dispose need to be called on the JDialog and right now you're calling it on whatever class happens to hold this method (we currently have no idea what class this is). One way to get your JDialog is to get the source object from the ActionCommand object via WindowsUtilities.getWindowAncestor, and then calling dispose on this.

    For example:

    Java Code:
    // private void windowAction(Object actionCommand) { //!! why Object???
    
    private void windowAction(ActionEvent actionCommand) {
            
            boolean closeWindow = false;
            String cmd = null;
                    
            if (actionCommand != null) {
                if (actionCommand instanceof ActionEvent) {
                    cmd = ((ActionEvent)actionCommand).getActionCommand();
                } else {
                    cmd = actionCommand.toString();
                }
            }
            
            if (cmd != null) {
                if (cmd.equals(CMD_CANCEL)) {
                    System.out.println("CANCEL");
                    closeWindow = true;
                } else if (cmd.equals(CMD_OK)) {
                    System.out.println("OK");
                    closeWindow = true;
                } else {
                    System.out.println("Command invoked: " + cmd);
                }
            }
            if (closeWindow) {
                Window win = SwingUtilities.getWindowAncestor((Component) actionCommand.getSource());        
                //setVisible(false);
                win.dispose();
            }
        }

  3. #3
    sky
    sky is offline Member
    Join Date
    Nov 2009
    Posts
    96
    Rep Power
    0

    Default

    You are right, that was the problem. I solved it by creating a new class called CustomDialog, and inside that class setVisible method can be used with no problem. I have two last questions: why do you write the comment "//!! why Object???" and how can I set the position of the Dialog in order to put it in the center of the parent frame ? Thank you very much for the quick answer.

  4. #4
    aaroncarpet's Avatar
    aaroncarpet is offline Senior Member
    Join Date
    Nov 2009
    Location
    California
    Posts
    147
    Rep Power
    0

    Default

    if you look in the api JDialog inherits alot of methods ......so think about the math first

    if you know the width and height of the parent window....

    if you know the widthand height of the dialog....

    if you subtract the width of the dialog from the width of parent window and divide by 2 ...

    I am only going to get you started on the code you need

    Java Code:
    JFrame f = new JFrame();
    //assume the frame is deployed ill skip the code
    JDialog d = new JDialog();
    /*its time to post your dialog and you want to center it...remember all methods from component and container are inherited*/
    {
    Dimension frame = new Dimension();
    Dimension dialog = new Dimension();
    int frwidth =0;
    int frhgt=0;
    int dwdth=0;
    int dheight=0;
    
    frame = f.getSize();;
    frhgt = frame.getHeight();
    frwidth = frame.getWidth();
    
    // continue to get your width and height of the dialog
    
    d.setLocation( "framewidth - dialog width /2" , "frameheight - dialogheight /2 (and maybe you have to add another number here");

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

    Default

    Quote Originally Posted by sky View Post
    why do you write the comment "//!! why Object???"
    Just that. Why use a non-specific Object for the parameter type, forcing you to cast the parameter as an ActionEvent rather than just use ActionEvent from the get-go. If you want two similar methods, one using a String the other an ActionEvent, then create two overloaded methods.

    and how can I set the position of the Dialog in order to put it in the center of the parent frame ?
    Much simpler than the post above is to just set the dialog's location to be relative to the JFrame via the setLocationRelativeTo(...). For example:
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class CenterDialog {
       private static void createAndShowUI() {
          JPanel panel = new JPanel();
          panel.setPreferredSize(new Dimension(600, 600));
          
          final JFrame frame = new JFrame("CenterDialog");
          frame.getContentPane().add(panel);
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.pack();
          frame.setLocation(600, 50);
          frame.setVisible(true);
          
          JPanel dlgPanel = new JPanel();
          dlgPanel.setPreferredSize(new Dimension(300, 300));
          final JDialog dialog = new JDialog(frame, "Dialog", true);
          dialog.getContentPane().add(dlgPanel);
          dialog.pack();
          dialog.setLocationRelativeTo(frame);
          dialog.setVisible(true);
       }
    
       public static void main(String[] args) {
          java.awt.EventQueue.invokeLater(new Runnable() {
             public void run() {
                createAndShowUI();
             }
          });
       }
    }
    Thank you very much for the quick answer.
    You're very welcome

  6. #6
    sky
    sky is offline Member
    Join Date
    Nov 2009
    Posts
    96
    Rep Power
    0

    Default

    I've tried both methods. For the first one, I wrote this code in the class that extends JDialog:

    Java Code:
    Dimension frameDimension = new Dimension();
    Dimension dialogDimension = new Dimension();
           
    frameDimension = frame.getSize();
    dialogDimension = getSize();
            
    int fHeight = (int) frameDimension.getHeight();
    int fWidth = (int) frameDimension.getWidth();
    int dHeight = (int) dialogDimension.getHeight();
    int dWidth = (int) dialogDimension.getWidth();
    
    setLocation((fWidth-dWidth)/2, (fHeight-dHeight)/2);
    but it didn't work. I've read in the Java reference that sometimes the method setLocation doesn't work because it depends on the operating system. Could be that the problem?

    With the second method, I simply add the line setLocationRelativeTo(frame) and it works, but not in the way I expected, because it sets in the center of the frame the top left corner of the dialog, but not the whole dialog. Thanks again for both answers.

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

    Default

    Quote Originally Posted by sky View Post
    I've tried both methods. For the first one, I wrote this code in the class that extends JDialog:

    but it didn't work. I've read in the Java reference that sometimes the method setLocation doesn't work because it depends on the operating system. Could be that the problem?

    With the second method, I simply add the line setLocationRelativeTo(frame) and it works, but not in the way I expected, because it sets in the center of the frame the top left corner of the dialog, but not the whole dialog. Thanks again for both answers.
    Without seeing compilable code, it's very hard to guess where your error is,... but it's certainly not in the code you've posted. I can take a wild guess, such that you're calling setLocationRelativeTo before calling pack, but again, who knows?
    Last edited by Fubarable; 11-16-2009 at 06:30 PM.

  8. #8
    sky
    sky is offline Member
    Join Date
    Nov 2009
    Posts
    96
    Rep Power
    0

    Default

    You were right again. I was calling setLocationRelativeTo() before calling pack(). Summing up:

    For center a dialog, this works:
    Java Code:
    pack();
    setLocationRelativeTo(frame);
    setVisible(true);
    and this one doesn't:
    Java Code:
    setLocationRelativeTo(frame);
    pack();
    setVisible(true);
    Thank you very much

Similar Threads

  1. JDialog appearing in the toolbar
    By riddick in forum New To Java
    Replies: 6
    Last Post: 11-06-2009, 02:19 PM
  2. setLocation on a JDialog is ignored
    By ScottVal in forum AWT / Swing
    Replies: 7
    Last Post: 01-13-2009, 07:35 AM
  3. help with jdialog
    By leonard in forum AWT / Swing
    Replies: 1
    Last Post: 08-05-2007, 05:37 AM
  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
  •