Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

getInsets() and Frames

 
Diana Liah
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Has anyone else had a problem getting correct values for border sizes when using getInsets() inside Frames? I have a Frame to which I am adding a container, but despite my use of getInsets().top (etc.) the component is still partially obscured when the Frame is shown.
I have the following code to try to set the Frame to its correct size:
setSize(getInsets().left + cont.getSize().width + getInsets().right, getInsets().top + cont.getSize().height + getInsets().bottom);
It does not seem to matter whether this code is placed within the Frame's constuctor or in a method that is called afterwards. I have tried printing the insets to the standard output, and they all come out to be "0" for some reason.
Can anyone explain this?
 
Rene Liebmann
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I made a little test with an JFrame, which holds only one button. When I asked for the Insets during initializint the JFrame, I got the same result like you, but on a Buttonclick I got a different result. I think, it is only possible to ask for those values if a component is really visible, I mean if you can see it.
And welcome to the JavaRanch.

Rene
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you read the APIs for the Frame class, you will find this interesting little tidbit...


The size of the frame includes any area designated for the border. The dimensions of the border area may be obtained using the getInsets method, however, since these dimensions are platform-dependent, a valid insets value cannot be obtained until the frame is made displayable by either calling pack or show. Since the border area is included in the overall size of the frame, the border effectively obscures a portion of the frame, constraining the area available for rendering and/or displaying subcomponents to the rectangle which has an upper-left corner location of (insets.left, insets.top), and has a size of width - (insets.left + insets.right) by height - (insets.top + insets.bottom).
 
Diana Liah
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rene and Nate,
Thank you for your help. I used pack() before setting the size, and it works perfectly.
Diana
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic