Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Center JDialog over parent

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.

like image 489
Chris Drappier Avatar asked Apr 05 '12 14:04

Chris Drappier


People also ask

How do I center my screen in JDialog?

If you really want to center a JDialog on screen, you can use code like this: // center a jdialog on screen JDialog d = new JDialog(); d. setSize(400, 300); d.

Where is JDialog used in an application?

JDialog is one of the important features of JAVA Swing contributing to interactive desktop-based applications. This is used as a top-level container on which multiple lightweight JAVA swing components can be placed to form a window based application.

What is a JDialog?

JDialog is a part Java swing package. The main purpose of the dialog is to add components to it. JDialog can be customized according to user need . Constructor of the class are: JDialog() : creates an empty dialog without any title or any specified owner.


2 Answers

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.

like image 140
MB. Avatar answered Oct 13 '22 22:10

MB.


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();         });     } } 
like image 20
Andrew Thompson Avatar answered Oct 13 '22 23:10

Andrew Thompson