GridBagLayout Resizing My Components?

J

Jason Cavett

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!
 
K

Knute Johnson

Jason said:
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.
 
D

Daniele Futtorovic

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/
 
D

Daniele Futtorovic

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.



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.



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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top