Solved

Placing JFrame in the center of screen.

Posted on 2004-04-13
5
532 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 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

What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

Question has a verified solution.

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

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
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:
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.

695 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