I'm using Swing GroupLayout and I'm confused about the values GroupLayout.DEFAULT_SIZE
and GroupLayout.PREFERRED_SIZE
. I never know when to use each one of them in methods like GroupLayout.addComponent(Component, int, int, int)
.
suppose I have this code:
GroupLayout l = ...;
l.setHorizontalGroup(l.createSequentialGroup()
.addComponent(tf1)
.addComponent(tf2));
l.setVerticalGroup(l.createParallelGroup()
.addComponent(tf1)
.addComponent(tf2));
there are two JTextField
s on a single line laid out with GroupLayout
(one sequential group horizontally and one parallel group vertically). if I resize the window now, both components get the available space (50% each). but I want only the first text field to grow/shrink horizontally and only the second text field to grow/shrink vertically. what values of min, pref and max should I use to accomplish that? I know I can just try it and see what combination works but I'd like to know the reasoning behind this problem.
Some guidance may be found in How to Use GroupLayout: Component Size and Resizability. Regarding DEFAULT_SIZE
and PREFERRED_SIZE
,
They can be used as parameters in the method
addComponent(Component comp, int min, int pref, int max)
To force a component to be resizable (allow shrinking and growing):
group.addComponent(component, 0, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
This allows the component to resize between zero size (minimum) to any size (
Short.MAX_VALUE
as maximum size means "infinite"). If we wanted the component not to shrink below its default minimum size, we would useGroupLayout.DEFAULT_SIZE
instead of 0 in the second parameter.To make a component fixed size (suppress resizing):
group.addComponent(component, GroupLayout.PREFERRED_SIZE,
GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)...
Interestingly, the constant values are negative, so they cannot be mistaken for actual constraints.
I was also confused about how GroupLayout.DEFAULT_SIZE
and GroupLayout.PREFERRED_SIZE
are used in GroupLayout.SequentialGroup.addComponent(Component c, int min, int pref, int max)
, even after referencing the GroupLayout section of the Java Tutorials and a java.net article titled Getting to know GroupLayout, part 1.
Diving into the JDK 1.6.0_27 GroupLayout.javasource I found the answers in the ComponentSpring class. From that I was able to work out these rules:
If a minimum size is needed:
min
value is non-negative, that value is returned.PREFERRED_SIZE
, we follow the rules for preferred size.If a preferred size is needed:
pref
value is non-negative, that value is returned.DEFAULT_SIZE
or PREFERRED_SIZE
, return the component's preferred size.If a maximum size is needed:
max
value is non-negative, that value is returned.PREFERRED_SIZE
, we follow the rules for preferred size.As trashgod already noted, the defines are negative. Any other negative value for min, pref and max besides DEFAULT_SIZE
or PREFERRED_SIZE
are errors and hit asserts.
The interplay between the SequentialGroup.addComponent
min, pref, and max sizes wasn't immediately apparent to me from the tutorial. Now I know why PREFERRED_SIZE,DEFAULT_SIZE,PREFERRED_SIZE
is fixed, why it doesn't matter if the middle argument is DEFAULT_SIZE
or PREFERRED_SIZE
and how the NetBeans generated fixed size values like DEFAULT_SIZE,300,Short.MAX_VALUE
work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With