Results 1 to 4 of 4
  1. #1
    javaexplorer is offline Member
    Join Date
    Jan 2010
    Posts
    20
    Rep Power
    0

    Default How to position a modal dialog box at the center of the screen?

    Hi,

    The modal dialog box is always getting displayed at the top-left hand corner of the screen. I have tried using setLocationRelativeTo(null), but it only serves to position the originating point of the dialog box at the center point of the launching window.

    Here is the code for my dialog box class:

    Java Code:
     class MyDialog extends JDialog {     
    
         
         public MyDialog(Frame launchingWindow, BufferedImage image) {
             
             super(launchingWindow, "Enlarged view of the selected image", 
                     true); //launch this as a modal dialog.
    
             JLabel imageDisplayer = new JLabel(new ImageIcon(image));
    
             this.add(imageDisplayer, BorderLayout.CENTER);
                
             this.pack();
                
         }
         
         
     }

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

    Default

    It works fine for me. Please create an SSCCE that shows your problem.

    e.g.,
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.image.BufferedImage;
    import java.io.IOException;
    import java.net.URL;
    import javax.imageio.ImageIO;
    import javax.swing.*;
    
    public class TestMyDialog {
    
      private static final String IMAGE_PATH = "http://duke.kenai.com/misc/Bullfight.jpg";
    
      private static void createAndShowGUI() {
        try {
          final JFrame frame = new JFrame("Test MyDialog");
    
          URL imageUrl = new URL(IMAGE_PATH);
          BufferedImage image = ImageIO.read(imageUrl);
          final MyDialog dialog = new MyDialog(frame, image);
          
          JButton launchDialog = new JButton("Launch Dialog");
          launchDialog.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
              dialog.setVisible(true);
            }
          });
          
          JPanel panel = new JPanel();
          panel.add(launchDialog);
          panel.setPreferredSize(new Dimension(400, 300));
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.getContentPane().add(panel);
          frame.pack();
          frame.setVisible(true);
          
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    
      public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
          public void run() {
            createAndShowGUI();
          }
        });
      }
    }
    
    class MyDialog extends JDialog {
    
      public MyDialog(Frame launchingWindow, BufferedImage image) {
    
        super(launchingWindow,
            "Enlarged view of the selected image", true); //launch this as a modal dialog.
        JLabel imageDisplayer = new JLabel(new ImageIcon(image));
        this.add(imageDisplayer, BorderLayout.CENTER);
        pack();
        setLocationRelativeTo(null);
      }
    
    }

  3. #3
    javaexplorer is offline Member
    Join Date
    Jan 2010
    Posts
    20
    Rep Power
    0

    Default

    Many thanks for your quick reply.

    After looking at your code, I have realized my folly. I was calling setLocationRelativeTo(null) before calling pack.

    I have amended the code and it is working fine now. Thanks :)

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

    Default

    Quote Originally Posted by javaexplorer View Post
    Many thanks for your quick reply.

    After looking at your code, I have realized my folly. I was calling setLocationRelativeTo(null) before calling pack.

    I have amended the code and it is working fine now. Thanks :)
    You're welcome, but you know of course that there was absolutely no way we could have guessed what your true problem was based on your question and the code that you posted. Next time you run into a problem, you'll get a quicker and better solution if you post all relevant code in an SSCCE.

    Much luck.

Similar Threads

  1. How to make a modal JFrame?
    By surot in forum New To Java
    Replies: 20
    Last Post: 06-12-2011, 12:31 AM
  2. JFrame at Center Screen
    By hiranya in forum AWT / Swing
    Replies: 8
    Last Post: 02-11-2010, 05:29 PM
  3. get position in string from caret position
    By helloworld111 in forum AWT / Swing
    Replies: 5
    Last Post: 02-19-2009, 02:36 AM
  4. Can't edit JTextField after modal dialog
    By JavaNerd in forum Java Applets
    Replies: 0
    Last Post: 02-07-2008, 10:11 PM
  5. Replies: 4
    Last Post: 06-08-2007, 07:15 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
  •