Java Swing Tutorial - Java Swing GroupLayout








The GroupLayout is in the javax.swing package.

A GroupLayout uses the concept of a group. A group consists of elements. An element of a group may be a component, a gap, or another group.

A gap is an invisible area between two components.

There are two types of groups:

  • Sequential group
  • Parallel group

When the elements in a group are placed in series, one after another, it is called a sequential group.

When the elements in a group are placed in parallel, it is called a parallel group.

The GroupLayout class contains three inner classes: Group, SequentialGroup, and ParallelGroup.

Group is an abstract class and the other two classes are inherited from the Group class.

We can use the factory methods of the GroupLayout class to create their objects.

The GroupLayout class provides two separate methods to create groups: createSequentialGroup()and createParallelGroup().

The following code demonstrates how to use a GroupLayout to display two buttons side by side in a JFrame.

import java.awt.Container;
//  w  w w .  ja  v  a  2s.  c o  m
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class Main {
  public static void main(String[] args) {
    JFrame frame = new JFrame("GroupLayout");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container contentPane = frame.getContentPane();

    GroupLayout groupLayout = new GroupLayout(contentPane);

    contentPane.setLayout(groupLayout);

    JLabel label = new JLabel("Label");
    JButton b2 = new JButton("Second Button");
    
    groupLayout.setHorizontalGroup(groupLayout.createSequentialGroup()
        .addComponent(label).addComponent(b2));

    groupLayout.setVerticalGroup(groupLayout
        .createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(label)
        .addComponent(b2));

    frame.pack();
    frame.setVisible(true);
  }
}




Gap

We can add gap between two components.

import java.awt.Container;
//w  ww .j a  v a  2 s.c o m
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class Main {
  public static void main(String[] args) {
    JFrame frame = new JFrame("GroupLayout");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container contentPane = frame.getContentPane();

    GroupLayout groupLayout = new GroupLayout(contentPane);

    contentPane.setLayout(groupLayout);

    JLabel label = new JLabel("Label");
    JButton b2 = new JButton("Second Button");
    
    groupLayout.setHorizontalGroup(groupLayout.createSequentialGroup()
        .addComponent(label).addGap(5, 10,   50).addComponent(b2));

    groupLayout.setVerticalGroup(groupLayout
        .createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(label)
        .addComponent(b2));

    frame.pack();
    frame.setVisible(true);
  }
}




Example

The following code adds a RELATED preferred gap between b1 and b2:

import java.awt.Container;
//w w w .java  2s. c  om
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.LayoutStyle;

public class Main {
  public static void main(String[] args) {
    JFrame frame = new JFrame("GroupLayout");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container contentPane = frame.getContentPane();

    GroupLayout groupLayout = new GroupLayout(contentPane);

    contentPane.setLayout(groupLayout);

    JLabel label = new JLabel("Label");
    JButton b2 = new JButton("Second Button");
    
    groupLayout.setHorizontalGroup(groupLayout.createSequentialGroup()
        .addComponent(label).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED).addComponent(b2));

    groupLayout.setVerticalGroup(groupLayout
        .createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(label)
        .addComponent(b2));

    frame.pack();
    frame.setVisible(true);
  }
}

Example 2

import static javax.swing.GroupLayout.Alignment.BASELINE;
import static javax.swing.GroupLayout.Alignment.LEADING;
import static javax.swing.GroupLayout.Alignment.TRAILING;
// ww  w .  j av a  2 s . c  o m
import java.awt.Container;

import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JFrame;

public class Main {
  public static void main(String[] args) {
    JFrame frame = new JFrame("GroupLayout");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container contentPane = frame.getContentPane();

    GroupLayout groupLayout =
     new GroupLayout(contentPane); groupLayout.setAutoCreateGaps(true);
     groupLayout.setAutoCreateContainerGaps(true);
     contentPane.setLayout(groupLayout);

    JButton b1 = new JButton("Button 1");
    JButton b2 = new JButton("Button Second");
    JButton b3 = new JButton("3");
    JButton b4 = new JButton("4");

    groupLayout.setHorizontalGroup(groupLayout
        .createSequentialGroup()
        .addGroup(
            groupLayout.createParallelGroup(LEADING).addComponent(b1)
                .addComponent(b3))
        .addGroup(
            groupLayout.createParallelGroup(TRAILING).addComponent(b2)
                .addComponent(b4)));

    groupLayout.setVerticalGroup(groupLayout
        .createSequentialGroup()
        .addGroup(
            groupLayout.createParallelGroup(BASELINE).addComponent(b1)
                .addComponent(b2))
        .addGroup(
            groupLayout.createParallelGroup(BASELINE).addComponent(b3)
                .addComponent(b4)));

    frame.pack();
    frame.setVisible(true);
  }
}