Stops GroupLayout components when stretched vertically

Is there an easy way to get all (or most) of the components in a GroupLayout application NOT to stretch vertically? I know I can do this by forcing each component to use its preferred size when I add it, but this makes the code 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 the default and just specify the elements that I want to stretch?

Links - addComponent specification

+3
source share
2 answers

, GroupLayout, , , ParallelGroup. resizeable ParallelGroup false.

Javadoc ParallelGroup

, jspCasts . ParallelGroup , , .

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
+3

, . :

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);
    }
}
+1

Source: https://habr.com/ru/post/1761770/


All Articles