1

I am having one parent frame in my system and one dialog that I need to show on Click. But I need to show that dialog in the middle of the parent frame. All time. And that parent frame position may not be the same all the time. So how to set it all time in to the middle?

I am Working on Swing JFrame.

com.verve.visdashboard.NewOkCancelDialog cancelDialog = new com.verve.visdashboard.NewOkCancelDialog(new FrameOwnserPnael(), true);
cancelDialog.setLocationRelativeTo(new FrameOwnserPnael());
cancelDialog.setLocationByPlatform(true);
Kishan Bheemajiyani
  • 3,429
  • 5
  • 34
  • 68

3 Answers3

2

Just use setLocationRelativeTo() method for your JDialog.

JDialog d = new JDialog();
d.setLocationRelativeTo(COMPONENT);

here COMPONENT is parent frame.

alex2410
  • 10,904
  • 3
  • 25
  • 41
1

The basic code should be:

JDialog dialog = new CustomDialog(...);
dialog.pack();
dialog.setLocationRelativeTo( parentFrame );

The size of the dialog is (0, 0) until you use the pack() method so the dialog can't be centered properly.

camickr
  • 321,443
  • 19
  • 166
  • 288
0

Each time you create a new parent frame. Instead you should create your parent frame only once and use that object to pass it as parameter.

FrameOwnserPnael parentFrame = new FrameOwnserPnael();
com.verve.visdashboard.NewOkCancelDialog cancelDialog = new com.verve.visdashboard.NewOkCancelDialog(parentFrame, true);
cancelDialog.setLocationRelativeTo(parentFrame);
cancelDialog.setLocationByPlatform(true);
dARKpRINCE
  • 1,538
  • 2
  • 13
  • 22