Results 1 to 8 of 8
  1. #1
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    Location
    United States
    Posts
    343
    Rep Power
    5

    Default 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)

  2. #2
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    Default

    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:

    Java Code:
    c.setSize(MY_SIZE);
    c.setMinimumSize(MY_SIZE);
    c.setPreferredSize(MY_SIZE);
    c.setMaximum(MY_SIZE);
    and eventually something works.

    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.

  3. #3
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    Location
    United States
    Posts
    343
    Rep Power
    5

    Default

    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)

  4. #4
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    Default

    consider this code:
    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);
    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.

    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.

  5. #5
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    Location
    United States
    Posts
    343
    Rep Power
    5

    Default

    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)

  6. #6
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    Default

    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

  7. #7
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    I get why preferredSize() should be used on components, but what I don't get is when to use setSize().
    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.

    The only time you use setSize() is when you DON'T use a layout manager (although this approach should be avoided at all costs).

  8. #8
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    Location
    United States
    Posts
    343
    Rep Power
    5

    Default

    Ah alright. thank you :)
    "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)

Similar Threads

  1. [Help] setSize work wrong ???
    By dawp in forum New To Java
    Replies: 7
    Last Post: 11-05-2009, 10:30 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •