aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Setting frame to the correct size Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Setting frame to the correct size" Watch "Setting frame to the correct size" New topic
Author

Setting frame to the correct size

tom davies
Ranch Hand

Joined: Apr 27, 2012
Posts: 168
I have a JFrame 400x400 and i want to center a number of components in the middle. The problem is that setting the JFrame to 400x400 includes the title bar etc, i want the content area to be 400x400 excluding title bars.
I have tried to get the size of the insets and add this to the jFrame size but it didnt seem to work. I have seen the pack() method but was wondering if there a way to do it without?

Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 1939
    
  28
Call pack() before you try to get the insets.
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

Any particular reason why you want to set it to exactly that size? Your frame is resizable as I can see, so insets being included in width/height of it doesn't really make such a big difference.


The quieter you are, the more you are able to hear.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
try it like this and it'll be centered with any resize

tom davies
Ranch Hand

Joined: Apr 27, 2012
Posts: 168
I just called the pack method before the insets and it all works fine now.
Kemal, its part of an exercise, one of the requirements is that a component of a certain size must be centred into a frame of a certain size.
Michael, in my Clock class i set the co ordinates of the clock taking into consideration the frame size, so it will always be centred anyway.
Thanks for the help
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

Kemal, its part of an exercise, one of the requirements is that a component of a certain size must be centred into a frame of a certain size.

Yes, but when one says the frame is of size WIDTH x HEIGHT it usually includes window title bar, borders, etc. You might set your frame to be undecorated to remove those so WIDTH x HEIGHT would be what you are looking for, if that works for you.
Though, it would be really better to set the size of the container that contains your component (Clock, or whatever) and then invoke pack() on your frame.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Setting frame to the correct size
 
Similar Threads
Virtual Graphing Calculator
new process in internal frame
Swing borders
Painting with a custom JComponent
Trying to resolve some odd resizing behavior of objects in GridBagLayout