Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

getPreferredSize() works on JFrame, why doesn't it work on Frame ?  RSS feed

 
raymond hsu
Greenhorn
Posts: 23
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, hope to get some help for a problem that confuse me.

I tested getPreferredSize() on JFrame and on Frame, getPreferredSize() returns all 0 (both width and height) for Frame, while in JFrame it returns non-zero numbers, which is working.

I am not sure which part of my coding in nolayout() method such that getPreferredSize() simply returns 0, and isPreferredSizeSet()) returns FALSE, implies PreferredSize not set.

Why is it not set ? Can anyone help? Thanks.

Below are the codings of this simple test program.


the output on the command window is as follows :

C:\JAVAPR>java test
size width is 55 height is 26
label size width is 88 height is 16
its left, 0 right, 0 top 0 bottom 0
is l1 preferred size set false
label 1 width 0 height 0
Text 1 width 0 height 0

 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A couple of things about your post. First, thanks for trying to use code tags, but you don't have it quite right yet. The tags should surround the code. Use the preview button to check if you have it right.

Second, this is a Swing question, so you'll get more responses in that forum. I'll move you post for you.
 
raymond hsu
Greenhorn
Posts: 23
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Snortum for the reminder of proper indentation in the code.

I didn't preview before submitting my question, will definitely do it next time.

I have corrected the missing indent, because while creating the java program in an editor, some lines I typed in are spaces instead of indent.

However, I couldn't find the insert <indent> key in coderanch editing icons, so I copy existing <indent> in my code to correct those lines not indented corrected.

Thanks for moving the post to swing, I will look up for any help reply in swing forum.
 
Rob Camick
Ranch Hand
Posts: 2800
15
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Frame is AWT. JFrame is Swing. The behaviours are different.

Why do you care? What is the point of using getPreferredSize()? What problem are you trying to solve? Rarely would you ever need to use that method since the size of components should be left up to the layout manager.

When you use a null layout panels don't have a preferred size.

Components don't have a size until the frame has been packed or made visible.
 
raymond hsu
Greenhorn
Posts: 23
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Camick said
"Components don't have a size until the frame has been packed or made visible. "

Thanks for the advise,

I tested by moving f.setVisible() immediately after the line f.setLayout(null),
now it works perfectly,
f.getInsets() returns the correct border thickness,
component.getPreferredSize() returns the correct width, and height.

AWT frame does behave differently from Swing JFrame.

Thanks.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!