-2

Before I initialized my dialog as

  addQuestionDialog = new JDialog(SwingUtilities.windowForComponent(this),"Add    question);

and I set the location of the dialog to be at the center of its parent by calling:

addQuestionDialog.setLocationRelativeTo(this)

This works and displays the dialog at the center of its parent, however when I set the dialog to be a modal dialog, it completely ignores the set method and displays the dialog in the top left corner of my screen.

addQuestionDialog = new JDialog(SwingUtilities.windowForComponent(this),"Add question", Dialog.ModalityType.DOCUMENT_MODAL);
Andrew Brick
  • 276
  • 2
  • 4
  • 18
  • Go to [http://stackoverflow.com/questions/213266/how-do-i-center-a-jdialog-on-screen](http://stackoverflow.com/questions/213266/how-do-i-center-a-jdialog-on-screen) – JBaba Mar 31 '15 at 18:31
  • 1
    I know how to re-position a dialog, the issue is that a modal dialog ignores my re-positioning, whereas a modeless dialog works perfectly fine – Andrew Brick Mar 31 '15 at 18:36

2 Answers2

2

however when I set the dialog to be a modal dialog, it completely ignores the set method and displays the dialog in the top left corner of my screen.

The order of the code should be:

dialog.setLocationRelativeTo(..);
dialog.setVisible(true );

I'm guessing you are using:

dialog.setVisible(true );
dialog.setLocationRelativeTo(..); // this is not executed until the dialog is closed.
camickr
  • 321,443
  • 19
  • 166
  • 288
0

This JDialog code sequence works for me:

   setModal(true)
   pack()
   setLocationRelativeTo(frame)
   setVisible(true)

Placing the setLocationRelativeTo(frame) line before the pack() line results in the off-center placement of the dialog. Interesting.:)