[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 536
  • Last Modified:

Placing JFrame in the center of screen.

hi!

I am trying to my JFrame wizard to be center of screen when I run it.
This works, but the size of frame get change to tiny frame!!
Any ideas?

**************************
...
...
    wizardFrame.setSize(500, 400);
    wizardFrame.setVisible(true);
...
...
Dimension screensize=Toolkit.getDefaultToolkit().getScreenSize();
int preferredWidth = screensize.width - ((screensize.width * 10)/100);
int preferredHeight = screensize.height - ((screensize.height * 10)/100);
wizardFrame.setBounds(((screensize.width - preferredWidth)/2), ((screensize.height - preferredHeight)/2),(screensize.width-1000),(screensize.height-1000));
wizardFrame.setLocationRelativeTo( null );
0
dkim18
Asked:
dkim18
  • 3
  • 2
2 Solutions
 
Tommy BraasCommented:
>> int preferredWidth = screensize.width - ((screensize.width * 10)/100);
>> int preferredHeight = screensize.height - ((screensize.height * 10)/100);
Hmmm, odd calculation of frame size. This is where your problem is.
0
 
Tommy BraasCommented:
>> wizardFrame.setBounds(((screensize.width - preferredWidth)/2), ((screensize.height - preferredHeight)/2),(screensize.width-1000),(screensize.height-1000));
The call to setBounds() is apparently the last call for setting size right?

Example:
(screensize.width-1000) ->  1024 - 1000 = 24 pixels!

Not very big!!! How large do you want your frame to be? Have a default smallest size, and then make it larger if there is room for it on the screen.
0
 
OBCTCommented:
This is the way I center any frame I'm using.
You can either keep this as a seperate method or use it in an existing one.

    public void centerFrame()
    {
        Dimension d = Toolkit.getDefaultToolkit().getScreenSize();

        int w = 550;
        int h = 400;
        int x = ((d.width  - w) / 2);
        int y = ((d.height - h) / 2);

        Frame f = new Frame("Test");
        f.setBounds(x,y,w,h);
        f.setVisible(true);
    }
0
 
OBCTCommented:
:)
0
 
Tommy BraasCommented:
=-)
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now