javacodex.com
Java Examples
Java Examples
memu home questions

GroupLayout

GroupLayout is a layout manager that was developed for GUI builders such as Matisse, the GUI builder provided with the NetBeans IDE. Although the layout manager was originally designed to suit the GUI builder needs, it also works well for manual coding.

Source: (GroupLayoutExample.java)

 
import java.awt.Component;
import javax.swing.*;
import static javax.swing.GroupLayout.Alignment.*;
 
public class GroupLayoutExample {
 
  public static void main(String[] args) {
 
    JFrame.setDefaultLookAndFeelDecorated(true);
    JFrame frame = new JFrame("GroupLayoutExample Example");
    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
 
    JLabel label = new JLabel("Label:");;
    JTextField textField = new JTextField();
    JCheckBox checkBox1 = new JCheckBox("CheckBox1");
    JCheckBox checkBox2 = new JCheckBox("CheckBox2");
    JCheckBox checkBox3 = new JCheckBox("CheckBox");
    JCheckBox checkBox4 = new JCheckBox("CheckBox4");
    JButton findButton = new JButton("Button 1");
    JButton cancelButton = new JButton("Button 2");
 
    checkBox1.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
    checkBox2.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
    checkBox3.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
    checkBox4.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
 
    GroupLayout layout = new GroupLayout(frame.getContentPane());
    frame.getContentPane().setLayout(layout);
 
    layout.setAutoCreateGaps(true);
    layout.setAutoCreateContainerGaps(true);
 
    layout.setHorizontalGroup(layout.createSequentialGroup()
        .addComponent(label)
        .addGroup(layout.createParallelGroup(LEADING)
            .addComponent(textField)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(LEADING)
                    .addComponent(checkBox1)
                    .addComponent(checkBox3))
                .addGroup(layout.createParallelGroup(LEADING)
                    .addComponent(checkBox2)
                    .addComponent(checkBox4))))
        .addGroup(layout.createParallelGroup(LEADING)
            .addComponent(findButton)
            .addComponent(cancelButton))
    );
 
    layout.linkSize(SwingConstants.HORIZONTAL, findButton, cancelButton);
 
    layout.setVerticalGroup(layout.createSequentialGroup()
        .addGroup(layout.createParallelGroup(BASELINE)
            .addComponent(label)
            .addComponent(textField)
            .addComponent(findButton))
        .addGroup(layout.createParallelGroup(LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(BASELINE)
                    .addComponent(checkBox1)
                    .addComponent(checkBox2))
                .addGroup(layout.createParallelGroup(BASELINE)
                    .addComponent(checkBox3)
                    .addComponent(checkBox4)))
            .addComponent(cancelButton))
    );
 
    frame.pack();
    frame.show();
  }
}
 

Output:

$ java GroupLayoutExample
Output



Contact: javacodex@yahoo.com