?
Solved

Placing JFrame in the center of screen.

Posted on 2004-04-13
5
Medium Priority
?
533 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 140 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 360 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses
Course of the Month11 days, 15 hours left to enroll

752 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