Nested Groups in GroupLayout - Java Swing

Java examples for Swing:GroupLayout

Description

Nested Groups in GroupLayout

Demo Code

import static javax.swing.GroupLayout.Alignment.BASELINE;
import static javax.swing.GroupLayout.Alignment.LEADING;
import static javax.swing.GroupLayout.Alignment.TRAILING;

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("Nested Groups in 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);/* ww  w. j a  v  a  2 s .c o  m*/

    JButton b1 = new JButton("Button 1");
    JButton b2 = new JButton("this is a test 2");
    JButton b3 = new JButton("3");
    JButton b4 = new JButton("Button 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);
  }
}

Related Tutorials