java GroupLayout

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

GroupLayout

GroupLayout groups its components and places them in a Container hierarchically. The grouping is done by instances of the Group class.

Group is an abstract class and two concrete classes which implement this Group class are SequentialGroup and ParallelGroup.

SequentialGroup positions its child sequentially one after another where as ParallelGroup aligns its child on top of each other.

The GroupLayout class provides methods such as createParallelGroup() and createSequentialGroup() to create groups.

GroupLayout treats each axis independently. That is, there is a group representing the horizontal axis, and a group representing the vertical axis. Each component must exists in both a horizontal and vertical group, otherwise an IllegalStateException is thrown during layout, or when the minimum, preferred or maximum size is requested.

Nested Classes

Modifier and Type

Class

Description

static class

GroupLayout.Alignment

Enumeration of the possible ways ParallelGroup can align its children.

class

GroupLayout.Group

Group provides the basis for the two types of operations supported by GroupLayout: laying out components one after another (SequentialGroup) or aligned (ParallelGroup).

class

GroupLayout.ParallelGroup

It is a Group that aligns and sizes it's children.

class

GroupLayout.SequentialGroup

It is a Group that positions and sizes its elements sequentially, one after another.

Fields

Modifier and Type

Field

Description

static int

DEFAULT_SIZE

It indicates the size from the component or gap should be used for a particular range value.

static int

PREFERRED_SIZE

It indicates the preferred size from the component or gap should be used for a particular range value.

Constructors

GroupLayout(Container host)

It creates a GroupLayout for the specified Container.

Useful Methods

Modifier and Type

Field

Description

void

addLayoutComponent(Component component, Object constraints)

It notify that a Component has been added to the parent container.

void

addLayoutComponent(String name, Component component)

It notify that a Component has been added to the parent container.

GroupLayout.ParallelGroup

createBaselineGroup(boolean resizable, boolean anchorBaselineToTop)

It creates and returns a ParallelGroup that aligns it's elements along the baseline.

GroupLayout.ParallelGroup

createParallelGroup()

It creates and returns a ParallelGroup with an alignment of Alignment.LEADING

GroupLayout.ParallelGroup

createParallelGroup(GroupLayout.Alignment alignment)

It creates and returns a ParallelGroup with the specified alignment.

GroupLayout.ParallelGroup

createParallelGroup(GroupLayout.Alignment alignment, boolean resizable)

It creates and returns a ParallelGroup with the specified alignment and resize behavior.

GroupLayout.SequentialGroup

createSequentialGroup()

It creates and returns a SequentialGroup.

boolean

getAutoCreateContainerGaps()

It returns true if gaps between the container and components that border the container are automatically created.

boolean

getAutoCreateGaps()

It returns true if gaps between components are automatically created.

boolean

getHonorsVisibility()

It returns whether component visiblity is considered when sizing and positioning components.

float

getLayoutAlignmentX(Container parent)

It returns the alignment along the x axis.

float

getLayoutAlignmentY(Container parent)

It returns the alignment along the y axis.

Dimension

maximumLayoutSize(Container parent)

It returns the maximum size for the specified container.

Example

  1. public class GroupExample {  
  2.     public static void main(String[] args) {  
  3.         JFrame frame = new JFrame("GroupLayoutExample");  
  4.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  5.         Container contentPanel = frame.getContentPane();  
  6.         GroupLayout groupLayout = new GroupLayout(contentPanel);  
  7.   
  8.         contentPanel.setLayout(groupLayout);  
  9.   
  10.         JLabel clickMe = new JLabel("Click Here");  
  11.         JButton button = new JButton("This Button");  
  12.   
  13.         groupLayout.setHorizontalGroup(  
  14.                     groupLayout.createSequentialGroup()  
  15.                                 .addComponent(clickMe)  
  16.                                 .addGap(1020100)  
  17.                                 .addComponent(button));  
  18.         groupLayout.setVerticalGroup(  
  19.                      groupLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)  
  20.                                 .addComponent(clickMe)  
  21.                                 .addComponent(button));  
  22.         frame.pack();  
  23.         frame.setVisible(true);  
  24.     }  
  25. }  

Output:

Java Grouplayout 1

Example 2

  1. import java.awt.Container;  
  2. import javax.swing.GroupLayout;  
  3. import javax.swing.JButton;  
  4. import javax.swing.JFrame;  
  5. import static javax.swing.GroupLayout.Alignment.*;  
  6. public class GroupExample2 {  
  7.     public static void main(String[] args) {  
  8.         JFrame frame = new JFrame("GroupLayoutExample");  
  9.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  10.         Container myPanel = frame.getContentPane();  
  11.   
  12.         GroupLayout groupLayout = new GroupLayout(myPanel);  
  13.         groupLayout.setAutoCreateGaps(true);  
  14.         groupLayout.setAutoCreateContainerGaps(true);  
  15.         myPanel.setLayout(groupLayout);  
  16.   
  17.         JButton b1 = new JButton("Button One");  
  18.         JButton b2 = new JButton("Button Two");  
  19.         JButton b3 = new JButton("Button Three");  
  20.   
  21.         groupLayout.setHorizontalGroup(groupLayout.createSequentialGroup()  
  22.                 .addGroup(groupLayout.createParallelGroup(LEADING).addComponent(b1).addComponent(b3))  
  23.                 .addGroup(groupLayout.createParallelGroup(TRAILING).addComponent(b2)));  
  24.   
  25.         groupLayout.setVerticalGroup(groupLayout.createSequentialGroup()  
  26.                 .addGroup(groupLayout.createParallelGroup(BASELINE).addComponent(b1).addComponent(b2))  
  27.                 .addGroup(groupLayout.createParallelGroup(BASELINE).addComponent(b3)));  
  28.   
  29.         frame.pack();  
  30.         frame.setVisible(true);  
  31.     }  
  32. }  

Output:

Java Grouplayout 2