Results 1 to 8 of 8
Thread: When to use setSize()
- 05-28-2010, 11:07 PM #1
When to use setSize()
Hey. I was ambivalent about using setSize() or setPreferredSize() so I google'd it and came to this: Steer clear of Java pitfalls - JavaWorld - I get why preferredSize() should be used on components, but what I don't get is when to use setSize().
Thanks in advance."Experience is what you get when you don't get what you want" (Dan Stanford)
"Rise and rise again until lambs become lions" (Robin Hood)
- 05-28-2010, 11:12 PM #2
Senior Member
- Join Date
- Mar 2010
- Posts
- 266
- Rep Power
- 4
it all depends on which of these two the layout manager will end up using (and they all work a bit differently). When i want to make sure my component is the size i think it is, i do this:
and eventually something works.Java Code:c.setSize(MY_SIZE); c.setMinimumSize(MY_SIZE); c.setPreferredSize(MY_SIZE); c.setMaximum(MY_SIZE);
I think setSize() was there in 1.0 implementation, and they never had the heart to take it out, but it's pretty much deprecated.
- 05-28-2010, 11:26 PM #3
Oh. So even with JFrames/JPanels, use setPreferredSize()? Also, what do the minimumSize and maximumSize do? This is what I'm thinking: It cannot choose the size set on preferredSize because of space. So it chooses the number closest to minimum or maximum depending on the constraint?
"Experience is what you get when you don't get what you want" (Dan Stanford)
"Rise and rise again until lambs become lions" (Robin Hood)
- 05-28-2010, 11:44 PM #4
Senior Member
- Join Date
- Mar 2010
- Posts
- 266
- Rep Power
- 4
consider this code:
now suppose panel's width is 300 px. How much should it give to left and right? by default, ti goes to 50/50, so 150 and 150.Java Code:JPanel panel = new JPanel(); panel.setLayout(new BoxLayout(BoxLayout.X_AXIS, p)); JLabel left = new JLabel(); left.setMinimumSize (new Dimension(100, 10000)); left.setMinimumSize (new Dimension(200, 10000)); JLabel right = new JLabel(); panel.add(left); panel.add(right);
suppose you re-size the panel to 500px. now it will see that left component doesn't want to grow past 200, so left will have 200px and right - 300px.
panel 1000px => left=200px, right=800px
on the other hand, if the panel shrinks to 150px, left doesn't want to be smaller than 100, so left gets 100, and right get 50.
the 10,000 value for the height that the component wants is of course ignored altogether if the panel is smaller than that.
That's all great in theory... Consider BorderLayout, for instance. Its central component will take up all available space REGARDLESS of anything else.
In my practice, only BorderLayout works predictably, and you can make 70% of your UI using that. BoxLayout is pretty tricky, but does work sometimes unless you're trying to get fancy. If you really, really trying to make good-looking UI, you have to bite the bullet and use GridBagLayout, which is EXTREMELY cumbersome, but it works as an HTML <table> in the end and you can make pretty much anything using that.
- 05-29-2010, 12:07 AM #5
oooh that makes sense. Another dumb question :S
What do the alignment methods/fields do? Like setAlignmentX, setAlignmentY, BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT?
This is what my intuition says:
setAlignmentX: put something in a straight horizontal line?
setAlignmentY: put something in a straight vertical line?"Experience is what you get when you don't get what you want" (Dan Stanford)
"Rise and rise again until lambs become lions" (Robin Hood)
- 05-29-2010, 12:11 AM #6
Senior Member
- Join Date
- Mar 2010
- Posts
- 266
- Rep Power
- 4
actually quite similar: all depends on the layout manager.
for example if you have a horizontal BoxLayout that's 100px high, and it contains a component that's 50px high - where should it be vertically? top/bottom/center? alignmentX kind of stuff is supposed to control it.
bad news is, I personally never got it to work predictably for anything at all :( but experiment around and see what you get
- 05-29-2010, 07:18 AM #7
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,158
- Rep Power
- 5
You don't use setSize(). It is the job of the layout manager to set the size and location of a component based on the rules of the layout manager. So even if you set the size it will be ignored.I get why preferredSize() should be used on components, but what I don't get is when to use setSize().
The only time you use setSize() is when you DON'T use a layout manager (although this approach should be avoided at all costs).
- 05-29-2010, 03:26 PM #8
Similar Threads
-
[Help] setSize work wrong ???
By dawp in forum New To JavaReplies: 7Last Post: 11-05-2009, 09:30 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks