Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Setting frame to the correct size

 
tom davies
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2952
59
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Call pack() before you try to get the insets.
 
Kemal Sokolovic
Bartender
Posts: 825
5
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try it like this and it'll be centered with any resize

 
tom davies
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 825
5
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic