Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I center a JDialog on screen?

Tags:

java

swing

How do I go about positioning a JDialog at the center of the screen?

like image 265
Allain Lalonde Avatar asked Oct 17 '08 18:10

Allain Lalonde


People also ask

How do I open a JFrame in center?

Just click on form and go to JFrame properties, then Code tab and check Generate Center .

What is JDialog in Java?

The JDialog class is a subclass of the AWT java. awt. Dialog class. It adds a root pane container and support for a default close operation to the Dialog object . These are the same features that JFrame has, and using JDialog directly is very similar to using JFrame .


1 Answers

In Java 1.4+ you can do:

final JDialog d = new JDialog(); d.setSize(200,200); d.setLocationRelativeTo(null); d.setVisible(true); 

Or perhaps (pre 1.4):

final JDialog d = new JDialog(); d.setSize(200, 200); final Toolkit toolkit = Toolkit.getDefaultToolkit(); final Dimension screenSize = toolkit.getScreenSize(); final int x = (screenSize.width - d.getWidth()) / 2; final int y = (screenSize.height - d.getHeight()) / 2; d.setLocation(x, y); d.setVisible(true); 
like image 177
johnstok Avatar answered Sep 29 '22 04:09

johnstok