Edited with a SSCCE and a workaround
I want to programmatically change the location of a JDialog.
public class SSCCE {
public static void main(String[] pArgs) {
JDialog dialog = new JDialog();
dialog.setSize(300, 300);
dialog.setLocation(10, 10);
dialog.setVisible(true);
}
}
However, this is not working, the new dialog is always located in the center of the screen. I suspect it has something todo with my L&F (Windows 7 64bit, jdk 1.6_30), when setting the dialog to be undecorated,
public class SSCCEWorksButUndecorated {
public static void main(String[] pArgs) {
JDialog dialog = new JDialog();
dialog.setSize(300, 300);
dialog.setLocation(10, 10);
dialog.setUndecorated(true);
dialog.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
dialog.setVisible(true);
}
}
The location is excalty where I specified.
But I need a decorated dialog, has anyone else seen this problem before or am I missing something?
Workaround by settings location after setVisible() for modal dialogs
public class SSCCEWorkaroundForModalDialogs {
public static void main(String[] pArgs) {
final JDialog dialog = new JDialog();
dialog.setSize(300, 300);
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
dialog.setLocation(10, 10);
}
});
dialog.setVisible(true);
}
}
The move method documentation says:
I assume you can do a call to
revalidaite()before setting the dialog visible again.