Center the Dialog box

Hello friends,

i tried this code to center the JDialog but i didnt get.

Dimension dialogSize = this.getPreferredSize();

Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

setLocation(screenSize.width/2 - dialogSize.width/2,screenSize.height/2 - dialogSize.height/2);

plz help me guys........

[346 byte] By [MCA134a] at [2007-11-26 20:42:28]
# 1

> Hello friends,

>

> i tried this code to center the JDialog but i

> didnt get.

>

> ion dialogSize = this.getPreferredSize();

> Dimension screenSize =

> Toolkit.getDefaultToolkit().getScreenSize();

> setLocation(screenSize.width/2 -

> dialogSize.width/2,screenSize.height/2 -

> dialogSize.height/2);

>

>plz help me guys........

dialog.setLocationRelativeTo(null);

watfora at 2007-7-10 2:02:06 > top of Java-index,Desktop,Core GUI APIs...
# 2
setLocationRelativeTo(null)
kirillga at 2007-7-10 2:02:06 > top of Java-index,Desktop,Core GUI APIs...
# 3
Thanks watfor.
MCA134a at 2007-7-10 2:02:06 > top of Java-index,Desktop,Core GUI APIs...
# 4
Thanks kirillg.
MCA134a at 2007-7-10 2:02:06 > top of Java-index,Desktop,Core GUI APIs...
# 5

Here is an class that does what you wanna do, just call it with JDialog as an argument. Also added one constructor for JFrame.

import java.awt.Toolkit;

import javax.swing.JDialog;

import javax.swing.JFrame;

public class setLocationCenter {

public setLocationCenter(JFrame frame) {

Toolkit verktyg = Toolkit.getDefaultToolkit();

int xHorn = ((verktyg.getScreenSize().width - frame.getSize().width) /2);

int yHorn = ((verktyg.getScreenSize().height - frame.getSize().height) /2);

frame.setLocation(xHorn, yHorn);

}

public setLocationCenter(JDialog dialog) {

Toolkit verktyg = Toolkit.getDefaultToolkit();

int xHorn = ((verktyg.getScreenSize().width - dialog.getSize().width) /2);

int yHorn = ((verktyg.getScreenSize().height - dialog.getSize().height) /2);

dialog.setLocation(xHorn, yHorn);

}

}

prigasa at 2007-7-10 2:02:06 > top of Java-index,Desktop,Core GUI APIs...