Reputation: 1653
How can I open a JDialog
completely in the middle of the monitor?
I think I have to do something like this:
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
final int height = screenSize.height;
final int width = screenSize.width;
But now I don't know how to continue for to reach my target.
Upvotes: 1
Views: 129
Reputation: 96016
See public void setLocationRelativeTo(Component c)
:
If the
component
isnull
, or theGraphicsConfiguration
associated with this component isnull
, the window is placed in the center of the screen.
Then you need to.. myDialog.setLocationRelativeTo(null);
Upvotes: 2
Reputation: 285460
The same as for a JFrame or any other Window, you need to setLocationRelativeTo(null)
after packing it.
myDialog.add(everything);
myDialog.pack();
myDialog.setLocationRelativeTo(null); // this centers the window
myDialog.setVisible(true);
Upvotes: 3