Solved

Placing JFrame in the center of screen.

Posted on 2004-04-13
5
521 Views
Last Modified: 2012-06-27
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
Comment
Question by:dkim18
  • 3
  • 2
5 Comments
 
LVL 14

Expert Comment

by:Tommy Braas
Comment Utility
>> 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
 
LVL 14

Assisted Solution

by:Tommy Braas
Tommy Braas earned 35 total points
Comment Utility
>> 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
 
LVL 9

Accepted Solution

by:
OBCT earned 90 total points
Comment Utility
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
 
LVL 9

Expert Comment

by:OBCT
Comment Utility
:)
0
 
LVL 14

Expert Comment

by:Tommy Braas
Comment Utility
=-)
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now