35

I have a Java swing application with a button that produces a popup window when a certain action is performed. I'd like to align the center point of the popup window with the center point of the parent window when it is rendered. How can I calculate the x,y coordinates to plug into setLocation() for the popup window?

Just to clarify. I do not want the behavior of setLocationRelativeTo() because that sets the top-left pixel of the popup over the center pixel of the parent frame. I want to set the center pixel of the popup over the center pixel of the parent frame.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Chris Drappier
  • 5,280
  • 10
  • 40
  • 64

5 Answers5

64

On the JDialog you've created you should call pack() first, then setLocationRelativeTo(parentFrame), and then setVisible(true). With that order the JDialog should appear centered on the parent frame.

If you don't call pack() first, then setting the location relative to the parent doesn't work properly because the JDialog doesn't know what size it is at that point. It appears to take the size as 0 by 0, which results in the "top left pixel of the popup over the center pixel of the parent" positioning mentioned in a comment to one of the other answers.

MB.
  • 7,365
  • 6
  • 42
  • 42
54

setLocationRelativeTo ..this sets the top left pixel of the popup over the center pixel of the parent. ..

No it does not!

Centered Dialog

Each of the 3 dialogs popped by this simple example appears to be centered as far as I can see. I can only guess that the code is calling setLocationRelativeTo at the wrong time.

import javax.swing.*;

class CenterTheDialog {

    CenterTheDialog() {
        for (int ii=1; ii<4; ii++) {
            JFrame f = new JFrame("Frame " + ii);
            f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

            f.setSize(400,300);
            f.setLocationByPlatform(true);
            f.setVisible(true);

            JDialog d = new JDialog(f);
            d.setSize(300,200);
            d.setLocationRelativeTo(f);
            d.setVisible(true);
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            new CenterTheDialog();
        });
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
5

Window#setLocationRelativeTo

Jeffrey
  • 44,417
  • 8
  • 90
  • 141
  • this sets the top left pixel of the popup over the center pixel of the parent. I want to set the center pixel of the popup over the center pixel of the parent – Chris Drappier Apr 05 '12 at 15:14
  • 1
    @CrhsiDrappier Are you sure? `then the window is located in such a way that the center of the window coincides with the center of the component.` – Jeffrey Apr 05 '12 at 15:24
  • *"Are you sure?"* The OP is surely wrong, as shown in my example that expands your answer. OTOH they seem determined to keep their head buried deep in the sand, so.. (shrugs) – Andrew Thompson Apr 05 '12 at 21:51
4

You can utilize event e to get parent window. Use getWindowAncestor and e.getSource().

dialog.setLocationRelativeTo(SwingUtilities.getWindowAncestor((Component) e.getSource()))

sixtytrees
  • 1,156
  • 1
  • 10
  • 25
2

You could try passing the coordinates of the parent window and its size to the new window and adding the coordinates + 1/2 parent frame size in each axis - 1/2 of the popups x/y to center upon center.

Or..If you extend you could use setLocationRelativeTo(owner)

Hope this helps

Phil
  • 463
  • 3
  • 9
  • this sets the top left pixel of the popup over the center pixel of the parent. I want to set the center pixel of the popup over the center pixel of the paren – Chris Drappier Apr 05 '12 at 15:14
  • well if you decided to follow my formula you could just additionally minus 1/2 of the x/y popups size. – Phil Apr 05 '12 at 15:21
  • add your comments to your answer and I'll accept. It's not perfect, but I think I may have other things going on that are affecting the outcome. I don't really want to post my whole project here. thanks. – Chris Drappier Apr 05 '12 at 15:41
  • *"Or..If you extend you could use setLocationRelativeTo(owner) but it will be slightly off"* No need to extend it, or do on the fly calculations. See my answer. – Andrew Thompson Apr 05 '12 at 21:14