0

I have a "Login internal frame" displayed while launching the application.But the problem is it does'nt appear centered in the screen of the application.

My code to get the actual size of the desktop and setting location of the JInternalFrame is as follows

private void new_init() {

  LoginInternal login = new LoginInternal(jMenuBar1); 
  Dimension desktopSize = this.getSize();
  Dimension jInternalFrameSize = login.getSize();
  System.out.println("desktopSize: "+desktopSize+" jInternalFrameSize:" +jInternalFrameSize );
  jMenuBar1.setVisible(false);
  login.setLocation((desktopSize.width - jInternalFrameSize.width)/2,(desktopSize.height- jInternalFrameSize.height)/2);
  jDesktopPane1.add(login); 
  login.show();
}

My screen resolution is 1366x768. But according to the print I have put

"desktopSize: java.awt.Dimension[width=1024,height=768] jInternalFrameSize:java.awt.Dimension[width=398,height=286]"

I get the resolution as 1024x768. In pre-init code of JDesktopPane,I have set "setExtendedState (JFrame.MAXIMIZED_BOTH);"

Now I get the both opened in maximised mode but the login frame is not centered as it takes the centering logic with resolution "[width=1024,height=768]".If it takes my current screen resolution I think it will be centered.

I hope i made it clear. Where iam going wrong?

user3066876
  • 11
  • 1
  • 1
  • 2

1 Answers1

0

it does'nt appear centered in the screen of the application.

LoginInternal login = new LoginInternal(jMenuBar1); 
Dimension desktopSize = this.getSize();
Dimension jInternalFrameSize = login.getSize();

The default size of a component is (0, 0) when it is created. Can't tell if your constructor code sets a size or not.

I have a "Login internal frame" displayed while launching the application.

Don't use a JInternalFrame for this reason. Use a modal JDialog. The the basic code is:

JDialog dialog = new JDialog(...);
dialog.add(...);
dialog.pack();
dialog.setLocationRelativeTo( null );
dialog.setVisible( true );
camickr
  • 321,443
  • 19
  • 166
  • 288
  • Thanks but now I have used JInternalFrame throughout my application its difficult to change now. Is there anyway with the existing code? – user3066876 Dec 14 '13 at 17:39
  • `Is there anyway with the existing code?` - I don't know what your existing code looks like. I suggested what I thought the problem was. You need to calculate the location AFTER all components have a valid size. So you need to give your internal frame a size before you do the calculation. – camickr Dec 14 '13 at 18:06
  • Internal frame has a size "jInternalFrameSize:java.awt.Dimension[width=398,height=286]" What is the problem is this.getSize() -> gets the size as 1024x768 but my actual resolution is 1366x768 which is why the centering is getting wrong – user3066876 Dec 14 '13 at 19:28
  • @user3066876, post your [SSCCE](http://sscce.org/) that demonstrates the problem. – camickr Dec 14 '13 at 20:54