3

Is there an easy way to get all (or most) of the components in a GroupLayout application NOT stretch vertically? I know I can do it by forcing each component to it's preferred size when I add it, but that makes the code so much more verbose:

       .addGroup(layout.createSequentialGroup()
          .addComponent(oDevRadio)
          .addComponent(oInstRadio)
       )

Becomes

       .addGroup(layout.createSequentialGroup()
          .addComponent(oDevRadio,
                        GroupLayout.PREFERRED_SIZE,
                        GroupLayout.PREFERRED_SIZE,
                        GroupLayout.PREFERRED_SIZE)
          .addComponent(oInstRadio,
                        GroupLayout.PREFERRED_SIZE,
                        GroupLayout.PREFERRED_SIZE,
                        GroupLayout.PREFERRED_SIZE)
       )

Is there a way to set it as a default, and just specify the elements I want to be stretchable?

References - addComponent's spec

zigdon
  • 14,573
  • 6
  • 35
  • 54

2 Answers2

3

As far as I know, the only method of telling GroupLayout components not to stretch or otherwise be misaligned requires that the relevant components be inside a ParallelGroup. It is then a simple matter to set the resizeable flag of the ParallelGroup to false.

Javadoc of ParallelGroup creator with relevant flag

For example, in the following code jspCasts is a very tall component. Without a new ParallelGroup with the flag being set to false, components next to it would either stretch or not align neatly as they should.

vGroup.addGroup(gl.createParallelGroup(Alignment.LEADING).
    addComponent(jspCasts).
    addGroup(gl.createParallelGroup(Alignment.CENTER, false).
      // without worrying about vertical stretching or misalignment, 
      // add your components here
Arvanem
  • 1,043
  • 12
  • 22
1

Not as far as I know. I've handled it with a utility class:

package alpha;

import java.awt.Component;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Group;

public class GroupLayoutUtil
{
    public static GroupLayout.Group addPreferred(Group g, Component c)
    {
        return g.addComponent(c, GroupLayout.PREFERRED_SIZE, 
                GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE);
    }
}
Devon_C_Miller
  • 16,248
  • 3
  • 45
  • 71