GroupLayout is a LayoutManager that hierarchically group the components and arranges them in a Container. Grouping is done by using the instances of the Group class. It is generally used for developing a GUI ( Graphic User Interface) builders such as Matisse, the GUI builder provided with the NetBeans IDE. GroupLayout Class supports two types of groups:
- A sequential group positions its child elements sequentially, one after another.
- A parallel group aligns its child elements in different ways.
Constructor of the class:
- GroupLayout(Container host): It is used to create a GroupLayout for the specified Container.
Commonly Used Methods:
- addLayoutComponent(Component comp, Object cons): Notify that a Component has been added to the parent container.
- getHonorsVisibility(): Returns whether component visibility is considered when sizing and positioning components.
- maximumLayoutSize(Container parent): Returns the maximum size for the specified container.
- getLayoutAlignmentX(along horizontal axis): It returns the alignment along the x axis.
- minimumLayoutSize(Container parent): Returns the minimum size for the specified container.
- getLayoutStyle(): Returns the LayoutStyle used for calculating the preferred gap between components.
Below programs illustrate the use of GroupLayout class:
- The following program illustrates the use of GropuLayout by arranging JLabel components in a JFrame, whose instance class is “GroupLayoutDemo”. We create 2 JLabel components named “headerLabel“, “statusLabel” and create 3 JButton components named “btn1“, “btn2“, “btn3” then add them to the JFrame by using add() method. We set the size and visibility of the frame by using setSize() and setVisible() method. The layout is set by using setLayout() method.
// Java Program to illustrate the GroupLayout class import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
// creating a class GroupLayoutDemo public class GroupLayoutDemo {
// Declaration of objects
// of JFrame class
private JFrame mainFrame;
// Declaration of objects
// of JLabel class
private JLabel headerLabel, statusLabel, msglabel;
// Declaration of objects
// of JPanel class
private JPanel controlPanel;
// create a class GroupLayoutDemo
public GroupLayoutDemo()
{
// used to prepare GUI
prepareGUI();
}
public static void main(String[] args)
{
// Creating Object of "GroupLayoutDemo" class
GroupLayoutDemo GroupLayoutDemo = new GroupLayoutDemo();
// to show the group layout demo
GroupLayoutDemo.showGroupLayoutDemo();
}
private void prepareGUI()
{
// Initialization of object
// "mainframe" of JFrame class.
mainFrame = new JFrame( "Java GroupLayout Examples" );
// Function to set the
// size of JFrame.
mainFrame.setSize( 400 , 400 );
// Function to set the
// layout of JFrame.
mainFrame.setLayout( new GridLayout( 3 , 1 ));
// Initialization of object
// "headerLabel" of JLabel class.
headerLabel = new JLabel( "" , JLabel.CENTER);
// Initialization of object
// "statusLabel" of JLabel class.
statusLabel = new JLabel( "" , JLabel.CENTER);
// Function to set the
// size of JFrame.
statusLabel.setSize( 350 , 100 );
// to add action WindowListner in JFrame
mainFrame.addWindowListener( new WindowAdapter()
{
public void windowClosing(WindowEvent windowEvent)
{
System.exit( 0 );
}
});
// Initialization of object
// "controlPanel" of JPanel class.
controlPanel = new JPanel();
// Function to set the
// layout of JFrame.
controlPanel.setLayout( new FlowLayout());
// Adding Jlabel "headerlabel"
// on JFrame.
mainFrame.add(headerLabel);
// Adding JPanel "controlPanel"
// on JFrame.
mainFrame.add(controlPanel);
// Adding JLabel "statusLabel"
// on JFrame.
mainFrame.add(statusLabel);
// Function to set the visible of JFrame.
mainFrame.setVisible( true );
}
private void showGroupLayoutDemo()
{
// Function to set the text
// on the header of JFrame.
headerLabel.setText( "Layout in action: GroupLayout" );
// Creating Object of
// "Panel" class
JPanel panel = new JPanel();
// Function to set the size of JFrame.
panel.setSize( 200 , 200 );
// Creating Object of
// "layout" class
GroupLayout layout = new GroupLayout(panel);
// it used to set Auto
// Create Gaps
layout.setAutoCreateGaps( true );
// it used to set Auto
// Create Container Gaps
layout.setAutoCreateContainerGaps( true );
// Creating Object
// of "btn1" class
JButton btn1 = new JButton( "Button 1" );
// Creating Object of
// "btn2" class
JButton btn2 = new JButton( "Button 2" );
// Creating Object of "btn3" class
JButton btn3 = new JButton( "Button 3" );
// It used to set the
// Horizontal group
layout.setHorizontalGroup(layout.createSequentialGroup()
// Adding the JButton "btn1"
.addComponent(btn1)
// Adding the sequential Group
.addGroup(layout.createSequentialGroup()
// Adding the Parallel Group
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
// Adding the JButton "btn2"
.addComponent(btn2)
// Adding the JButton "btn3"
.addComponent(btn3))));
// set the vertical layout group
layout.setVerticalGroup(layout.createSequentialGroup()
// Adding the JButton "btn1"
.addComponent(btn1)
// Adding the JButton "btn2"
.addComponent(btn2)
// Adding the JButton "btn3"
.addComponent(btn3));
// Function to set the Layout of JFrame.
panel.setLayout(layout);
// Adding the control panel
controlPanel.add(panel);
// Function to set the visible of JFrame.
mainFrame.setVisible( true );
}
} |
Output:
- The following program illustrates the use of GropuLayout by arranging JLabel components in a JFrame, whose instance class is “GroupLayoutExample”. We create 1 JLabel , 1 JTextField and 2 JCheckbox components. Two JButton components are also created as “FindButton“, “CancelButton” and then add them to the JFrame by using add() method. The layout is set by using setLayout() method.
// Java Program to illustrate the GroupLayout class import java.awt.Component;
import javax.swing.*;
import static javax.swing.GroupLayout.Alignment.*;
// creating a class GroupLayoutExample public class GroupLayoutExample {
// Main Method
public static void main(String[] args)
{
// Function to set the Default Look
// And Feel Decorated of JFrame.
JFrame.setDefaultLookAndFeelDecorated( true );
// Creating Object of
// "JFrame" class
JFrame frame = new JFrame( "GroupLayoutExample" );
// Function to set the Default
// Close Operation of JFrame.
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
// Creating Object of "JLabel" class
JLabel label = new JLabel( "Label:" );
// Creating Object of
// "JTextField" class
JTextField textField = new JTextField();
// Creating Object of
// "JCheckBox" class
JCheckBox checkBox1 = new JCheckBox( "CheckBox1" );
// Creating Object of "JCheckBox" class
JCheckBox checkBox2 = new JCheckBox( "CheckBox2" );
// Creating Object of "JButton" class
JButton findButton = new JButton( "Button 1" );
// Creating Object of "JButton" class
JButton cancelButton = new JButton( "Button 2" );
// used to set the Border of a checkBox1
checkBox1.setBorder(BorderFactory.createEmptyBorder( 0 , 0 ,
0 , 0 ));
// used to set the Border of a checkBox2
checkBox2.setBorder(BorderFactory.createEmptyBorder( 0 , 0 ,
0 , 0 ));
// Creating Object of "GroupLayout" class
GroupLayout layout = new GroupLayout(frame.getContentPane());
// to get the content pane
frame.getContentPane().setLayout(layout);
// it used to set Auto Create Gaps
layout.setAutoCreateGaps( true );
// it used to set Auto Create Container Gaps
layout.setAutoCreateContainerGaps( true );
// it used to set the horizontal group
layout.setHorizontalGroup(layout.createSequentialGroup()
// Adding the label
.addComponent(label)
// Adding the Parallel Group
.addGroup(layout.createParallelGroup(LEADING)
// Adding the textfield
.addComponent(textField)
// Adding the Sequential Group
.addGroup(layout.createSequentialGroup()
// Adding the Parallel Group
.addGroup(layout.createParallelGroup(LEADING)
// Adding the checkBox1
.addComponent(checkBox1))
// Adding the Parallel Group
.addGroup(layout.createParallelGroup(LEADING)
// Adding the checkBox2
.addComponent(checkBox2))))
// Adding the Parallel Group
.addGroup(layout.createParallelGroup(LEADING)
// Adding the findButton
.addComponent(findButton)
// Adding the CancelButton
.addComponent(cancelButton)));
layout.linkSize(SwingConstants.HORIZONTAL,
findButton, cancelButton);
layout.setVerticalGroup(layout.createSequentialGroup()
// Adding the Parallel Group
.addGroup(layout.createParallelGroup(BASELINE)
// Adding the label
.addComponent(label)
// Adding the textField
.addComponent(textField)
// Adding the findButton
.addComponent(findButton))
// Adding the Parallel Group
.addGroup(layout.createParallelGroup(LEADING)
// Adding the sequential Group
.addGroup(layout.createSequentialGroup()
// Adding the Parallel Group
.addGroup(layout.createParallelGroup(BASELINE)
// Adding the checkBox1
.addComponent(checkBox1)
// Adding the checkBox2
.addComponent(checkBox2))
// Adding the Parallel Group
.addGroup(layout.createParallelGroup(BASELINE)))
// Adding the CancelButton
.addComponent(cancelButton)));
frame.pack();
frame.show();
}
} |
Output:
Note: The above programs might not run in an online IDE. Please use an offline compiler.
Reference: https://docs.oracle.com/javase/7/docs/api/javax/swing/GroupLayout.html