8

I am adding a bunch of JInternalFrames into a JDesktopPane, as the user selects to open various features through the menus. But I would like the internal frames to open centered in the desktop pane, as opposed to the upper left, where they seem to default.

How can I specify that the JInternalFrames open centered, or move them to the center after opening?

jDesktopPane.add(jInternalFrame); // jInternalFrame is not centered!
Nate W.
  • 9,141
  • 6
  • 43
  • 65
Steven
  • 920
  • 2
  • 9
  • 21
  • 1
    You should get the with and height of the screen, and make some math to set the X and Y for the JInternalFrame. I've not done that in years, so i can't give you the complete methods. Sorry – santiagobasulto Sep 20 '11 at 17:11

5 Answers5

15

For reference, here is the solution I used, based on dogbane's advice:

Dimension desktopSize = desktopPane.getSize();
Dimension jInternalFrameSize = jInternalFrame.getSize();
jInternalFrame.setLocation((desktopSize.width - jInternalFrameSize.width)/2,
    (desktopSize.height- jInternalFrameSize.height)/2);
Steven
  • 920
  • 2
  • 9
  • 21
4

Work out the top-left corner of the new location (based on the size of the JDesktopPane and JInternalFrame) and then call JInternalFrame.setLocation.

dogbane
  • 266,786
  • 75
  • 396
  • 414
1

If you are using Netbeans (which is recommended for desktop apps) you just need to:

  1. Select the form, right click and then properties;
  2. Go to code tab;
  3. Change "Form size policy" from "Generate Pack()" to "Generate Resize Code";
  4. Form Position (option above Form size policy) will be available.

Now you can set the for position as you wish :)

compt
  • 82
  • 2
  • 6
0

Add this void

public void addCentered(Component jif) {        
    desktopPane.add(jif);
    jif.setLocation((desktopPane.getWidth()-jif.getWidth())/2, (desktopPane.getHeight()-jif.getHeight())/2);
    jif.setVisible(true);
}

and when adding the jInternalFrame call:

addCentered(jifName);
evening_g
  • 158
  • 1
  • 8
0

I would suggest the Window.setLocationRelativeTo(Component) method, which will center the window relative to a specified component. Instead of passing in a JDesktopPane, you might want to obtain the parent frame for a component, since otherwise, your JInternalFrame will be centered according to whichever component you pass in.

Here is a code sample:

private void showDialog(Dialog dialogToCenter, Component button) {
    Frame frame = JOptionPane.getFrameForComponent(button);
    dialogToCenter.setLocationRelativeTo(frame);
    dialogToCenter.setVisible(true);
}
piepera
  • 2,033
  • 1
  • 20
  • 21
  • 2
    I believe setLocationRelativeTo is only available for Windows, not JInternalFrames (which don't inherit from Window). Maybe I am missing something, but can you use this method with a JInternalFrame instead of a JDialog? – Steven Sep 20 '11 at 18:12
  • Sorry, you're correct, I mistakenly thought that JInternalFrame extended from Window. – piepera Sep 21 '11 at 13:49