Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I open a JInternalFrame centered in a JDesktopPane?

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!
like image 518
Steven Avatar asked Sep 20 '11 17:09

Steven


3 Answers

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);
like image 136
Steven Avatar answered Oct 05 '22 14:10

Steven


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

like image 23
dogbane Avatar answered Oct 05 '22 13:10

dogbane


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 :)

like image 39
compt Avatar answered Oct 05 '22 14:10

compt