GridBagLayout Resizing My Components?

Discussion in 'Java' started by Jason Cavett, Jul 23, 2008.

  1. Jason Cavett

    Jason Cavett Guest

    I'm a little confused about GridBagLayout. I've used it quite a bit,
    and it works great about 95% of the time. I recently ran into this
    problem with two different components, and I am wondering if there's a
    simple solution.

    I have a bunch of components in a JPanel. Let's say the JPanel is a
    certain height and width. All the components have a preferred size
    (but not max or min explicitly defined) and everything looks great.

    Because of various restrictions previously, the width of the JPanel
    was way bigger than it needed to be. So, I recently reduced with
    width (just had to change a static final int at the top of the source
    code). I fire up the GUI again, and everything looks great...except
    one of the components (a JTextBox in one GUI and a JComboBox in
    another GUI) are now ~1px in width. Every other component looks
    perfectly fine.

    In fact, what's even more weird is that, in the case of the JComboBox,
    I changed the height. And, now, the JComboBox is the width of the
    longest piece of text inside the box rather than following its
    preferred size.

    I have to admit, I can't figure this out for the life of me. If I set
    minimumSize, it looks fine, but I'd like to understand what is
    happening.


    Thanks for any help to this frustrating problem!
    Jason Cavett, Jul 23, 2008
    #1
    1. Advertising

  2. Jason Cavett wrote:
    > I'm a little confused about GridBagLayout. I've used it quite a bit,
    > and it works great about 95% of the time. I recently ran into this
    > problem with two different components, and I am wondering if there's a
    > simple solution.
    >
    > I have a bunch of components in a JPanel. Let's say the JPanel is a
    > certain height and width. All the components have a preferred size
    > (but not max or min explicitly defined) and everything looks great.
    >
    > Because of various restrictions previously, the width of the JPanel
    > was way bigger than it needed to be. So, I recently reduced with
    > width (just had to change a static final int at the top of the source
    > code). I fire up the GUI again, and everything looks great...except
    > one of the components (a JTextBox in one GUI and a JComboBox in
    > another GUI) are now ~1px in width. Every other component looks
    > perfectly fine.
    >
    > In fact, what's even more weird is that, in the case of the JComboBox,
    > I changed the height. And, now, the JComboBox is the width of the
    > longest piece of text inside the box rather than following its
    > preferred size.
    >
    > I have to admit, I can't figure this out for the life of me. If I set
    > minimumSize, it looks fine, but I'd like to understand what is
    > happening.
    >
    >
    > Thanks for any help to this frustrating problem!


    This would be a really good one to have an SSCCE on.

    That being said, the 1 pixel sized component is that size because it
    can't be its preferred size for whatever reason. If you are not going
    to pack your containers (with everything in them) or you are going to
    allow the container to be resized, this is a risk without minimum sizes set.

    --

    Knute Johnson
    email s/nospam/knute2008/

    --
    Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
    ------->>>>>>http://www.NewsDemon.com<<<<<<------
    Unlimited Access, Anonymous Accounts, Uncensored Broadband Access
    Knute Johnson, Jul 23, 2008
    #2
    1. Advertising

  3. On 23/07/2008 22:29, Jason Cavett allegedly wrote:
    > I'm a little confused about GridBagLayout. I've used it quite a bit,
    > and it works great about 95% of the time. I recently ran into this
    > problem with two different components, and I am wondering if there's a
    > simple solution.
    >
    > I have a bunch of components in a JPanel. Let's say the JPanel is a
    > certain height and width. All the components have a preferred size
    > (but not max or min explicitly defined) and everything looks great.
    >
    > Because of various restrictions previously, the width of the JPanel
    > was way bigger than it needed to be. So, I recently reduced with
    > width (just had to change a static final int at the top of the source
    > code). I fire up the GUI again, and everything looks great...except
    > one of the components (a JTextBox in one GUI and a JComboBox in
    > another GUI) are now ~1px in width. Every other component looks
    > perfectly fine.


    I would surmise that:

    - the container is not sufficiently wide to layout all the
    components at their respective, preferred width (including layout
    elements like
    Insets);

    AND

    - the component in question has no (/the default) minimum size;

    AND

    (
    - the GridBagConstraints for the cell in question has .weightx == 0;
    OR
    - the GridBagConstraints for the cell in question has (.fill |
    GridBagContraints.HORIZONTAL) == 0;
    OR
    - both
    )


    When designing a GridBagLayout, it's better to configure each cell
    according to how it should behave or "what it should be" -- as opposed
    to merely doing the least necessary to make it look fine.


    > In fact, what's even more weird is that, in the case of the JComboBox,
    > I changed the height. And, now, the JComboBox is the width of the
    > longest piece of text inside the box rather than following its
    > preferred size.


    Is it on the same "row" as the Component that is now at 1px width? If
    yes, I would suppose its GridBagConstraints have .weightx > 0 and (.fill
    | GridBagConstraints.HORIZONTAL != 0) -- consequently, it gets the extra
    screen estate.


    > I have to admit, I can't figure this out for the life of me. If I set
    > minimumSize, it looks fine, but I'd like to understand what is
    > happening.


    HTH.

    /Follow-up to c.l.j.gui/

    --
    DF.
    Daniele Futtorovic, Jul 23, 2008
    #3
  4. On 23/07/2008 23:58, Daniele Futtorovic allegedly wrote:
    > On 23/07/2008 22:29, Jason Cavett allegedly wrote:
    >> I'm a little confused about GridBagLayout. I've used it quite a bit,
    >> and it works great about 95% of the time. I recently ran into this
    >> problem with two different components, and I am wondering if there's a
    >> simple solution.
    >>
    >> I have a bunch of components in a JPanel. Let's say the JPanel is a
    >> certain height and width. All the components have a preferred size
    >> (but not max or min explicitly defined) and everything looks great.
    >>
    >> Because of various restrictions previously, the width of the JPanel
    >> was way bigger than it needed to be. So, I recently reduced with
    >> width (just had to change a static final int at the top of the source
    >> code). I fire up the GUI again, and everything looks great...except
    >> one of the components (a JTextBox in one GUI and a JComboBox in
    >> another GUI) are now ~1px in width. Every other component looks
    >> perfectly fine.

    >
    > I would surmise that:
    >
    > - the container is not sufficiently wide to layout all the components
    > at their respective, preferred width (including layout elements like
    > Insets);
    >
    > AND
    >
    > - the component in question has no (/the default) minimum size;
    >
    > AND
    >
    > (
    > - the GridBagConstraints for the cell in question has .weightx == 0;
    > OR
    > - the GridBagConstraints for the cell in question has (.fill |
    > GridBagContraints.HORIZONTAL) == 0;
    > OR
    > - both
    > )
    >
    >
    > When designing a GridBagLayout, it's better to configure each cell
    > according to how it should behave or "what it should be" -- as opposed
    > to merely doing the least necessary to make it look fine.
    >
    >
    >> In fact, what's even more weird is that, in the case of the JComboBox,
    >> I changed the height. And, now, the JComboBox is the width of the
    >> longest piece of text inside the box rather than following its
    >> preferred size.

    >
    > Is it on the same "row" as the Component that is now at 1px width? If
    > yes, I would suppose its GridBagConstraints have .weightx > 0 and (.fill
    > | GridBagConstraints.HORIZONTAL != 0) -- consequently, it gets the extra
    > screen estate.
    >
    >
    >> I have to admit, I can't figure this out for the life of me. If I set
    >> minimumSize, it looks fine, but I'd like to understand what is
    >> happening.

    >
    > HTH.
    >
    > /Follow-up to c.l.j.gui/


    Oh bummer. Can't seem to write a single post without a major blunder
    these days. The binary ORs in the above are supposed to be binary ANDs.

    --
    DF.
    Daniele Futtorovic, Jul 24, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Stéphanie Vanhove

    GridBagLayout: window resizing problem

    Stéphanie Vanhove, Nov 23, 2004, in forum: Java
    Replies:
    4
    Views:
    3,361
    Filip Larsen
    Nov 23, 2004
  2. David Egan

    Re: Resizing Form Components

    David Egan, Apr 26, 2004, in forum: HTML
    Replies:
    1
    Views:
    401
    brucie
    Apr 26, 2004
  3. asd
    Replies:
    5
    Views:
    1,271
  4. Pil (Trustworthy from Experience)

    Resizing a div by resizing its borders

    Pil (Trustworthy from Experience), Apr 18, 2009, in forum: Javascript
    Replies:
    9
    Views:
    346
    Proper
    Apr 21, 2009
  5. Proper
    Replies:
    0
    Views:
    196
    Proper
    Apr 18, 2009
Loading...

Share This Page