Create GroupLayout - Java Swing

Java examples for Swing:GroupLayout

Introduction

The GroupLayout is in the javax.swing package.

GroupLayout should be used by GUI builders.

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

Demo Code

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("Simplest GroupLayout");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container contentPane = frame.getContentPane();

    GroupLayout groupLayout = new GroupLayout(contentPane);

    contentPane.setLayout(groupLayout);// ww  w  .ja  va  2 s  . com

    JButton b1 = new JButton("Button 1");
    JButton b2 = new JButton("this is a test 2");

    groupLayout.setHorizontalGroup(groupLayout.createSequentialGroup()
        .addComponent(b1).addComponent(b2));

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

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

Related Tutorials