Solved

Placing JFrame in the center of screen.

Posted on 2004-04-13
5
524 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
ID: 10816893
>> 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
ID: 10816931
>> 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
ID: 10818020
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
ID: 10818228
:)
0
 
LVL 14

Expert Comment

by:Tommy Braas
ID: 10818308
=-)
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Protect jar file - windows app 2 53
GUI builder for Eclipse? 8 38
object oriented programming comparison 5 72
Selenium docs api java index 3 21
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
This video teaches viewers about errors in exception handling.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

825 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