Giovanni Far
Giovanni Far

Reputation: 1653

Open a JDialog in the middle of the monitor

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

Answers (2)

Maroun
Maroun

Reputation: 96016

See public void setLocationRelativeTo(Component c):

If the component is null, or the GraphicsConfiguration associated with this component is null, the window is placed in the center of the screen.

Then you need to.. myDialog.setLocationRelativeTo(null);

Upvotes: 2

Hovercraft Full Of Eels
Hovercraft Full Of Eels

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

Related Questions