What is the easiest way in standard Java Swing to align three components in such a way that:
-
the dynamic widths of
Component1andComponent3are adjusted to be equal, -
while
Component2(which is in between) has constant width?
Imagine we have some resizable JPanel (such as inside a JFrame).
Small width should look like this:
[----------------whole JPanel----------------]
[--Component1--] [Component2] [--Component3--]
Big width should look like this:
[------------------------whole JPanel------------------------]
[------Component1------] [Component2] [------Component3------]
Note: I just “trialed-and-errored” with GroupLayout for too long.
One option is using a GridBagLayout. A quick and dirty snippet to get you started:
Obviously there’s a lot more you can do with a GridBag, and you’ll need to do some polishing to get exactly what you want to see, but this should get you started.
The fifth parameter in the GBC constructor is a weight for stretching in the x direction. Setting them equal, and forcing the center component not to stretch (F_NONE) does the dirty work.