views:

32

answers:

1

hi,

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 JTextFields 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.

+2  A: 

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 use GroupLayout.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.

trashgod