2

I have my own main Frame and two JDialogs that pop up when someone chooses menu items.What i'd like is to have a Help dialog with some rules,suggestions etc. setLayout(null) and the Toolkit/Dimension do NOT help of course because they center the upper left corner of the frame/dialog in the screen . How could have the center of both the frame and dialog centered in a screen? Thanks in advance !

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
eli g
  • 35
  • 4
  • 1
    For frame positioning, you cannot go by `setLocationByPlatform(true)`. See [this answer](http://stackoverflow.com/a/7143398/418556) for demo. (It includes a combination of the two methods.) – Andrew Thompson Aug 10 '13 at 18:40

5 Answers5

3

Do these in order:

1) Call pack() or setSize(...) on the frame/dialog to be shown (it should already have its components added at this point).

2) Then, call setLocationRelativeTo(null) on that container. Alternatively, you could call setLocationRelativeTo(parent) if the parent is offset and you want the dialog to be centered within the parent at its current location.

3) Then, call setVisible(true)

splungebob
  • 5,357
  • 2
  • 22
  • 45
2

I think that will help:

frame.setLocationRelativeTo(null);

Usually we put a null as an argument, but you can put an argument of type Component, I prefer JOptionPane as it's always shows in center of the screen.

   frame.setLocationRelativeTo(new JOptionPane());
   //OR
   frame.setLocationRelativeTo(JOptionPane.getRootFrame());
  • And one hint for your, don't use null layout(absolute positioning), always use LayoutManagers.
Azad
  • 5,047
  • 20
  • 38
2

setLocationRelativeTo(null) will only center the upper left corner if you Dialog has no size yet, e.g. if you call the .pack() method after the setLocationRelativeTo.

Small example:

    JFrame testFrame = new JFrame();
    JButton testButton = new JButton();
    testButton.setPreferredSize(new Dimension(500, 500));
    testFrame.add(testButton);
    testFrame.pack();
    testFrame.setLocationRelativeTo(null);
    testFrame.setVisible(true);

This will show an Frame which center is centered on the screen.

Dennis Kriechel
  • 3,719
  • 14
  • 40
  • 62
1

Use this helper method to center any JFrame or JDialog:

public static Window centerFrame(Window win) {
        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
        win.setLocation((int) ((dim.getWidth() - win.getWidth()) / 2), (int) ((dim.getHeight()  - win.getHeight()) / 2));
        return win;
    }
Jk1
  • 11,233
  • 9
  • 54
  • 64
  • but that's what i said in the description. Dimension/toolkit and setLayout(null) both do the same thing but they center the north left corner of the frame/dialog. – eli g Aug 10 '13 at 18:26
  • No, this code takes window size into account, that is why it works. The only tricky moment here is that you should invoke it after pack(). Before pack() any Window has no computed size. – Jk1 Aug 10 '13 at 18:50
  • Thanks, but having setLayoutTo(null) after the pack() solved the problem with smaller code. :) – eli g Aug 10 '13 at 18:53
0

Try to add this to your code:
frameName.setLocationRelativeTo(null);
This will center the frame in your screen. One thing, make sure that you declare the size of your frame BEFORE this line of code. This line should be right before your setVisible declaration