Discuss group layout with suitable example.
GroupLayout
GroupLayout groups its components and place them in a Container hierarchically. The grouping is done by instances of the Group class. Group is an abstract class, and two concrete classes which implement this Group class are SequentialGroup and ParallelGroup. SequentialGroup positions its child sequentially one after another whereas ParallelGroup aligns its child on top of each other. The GroupLayout class provides methods such as createParallelGroup() and createSequentialGroup() to create groups.
GroupLayout treats each axis independently. That is, there is a group representing the horizontal axis, and a group representing the vertical axis. Each component must exist in both a horizontal and vertical group, otherwise, an IllegalStateException is thrown during layout or when the minimum, preferred, or maximum size is requested.
Example:
import java.awt.*;
import javax.swing.*;
public class GroupExample {
public static void main(String[] args) {
JFrame frame = new JFrame("GroupLayoutExample");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container contentPanel = frame.getContentPane();
GroupLayout groupLayout = new GroupLayout(contentPanel);
contentPanel.setLayout(groupLayout);
JLabel clickMe = new JLabel("Click Here");
JButton button = new JButton("This Button");
groupLayout.setHorizontalGroup(
groupLayout.createSequentialGroup()
.addComponent(clickMe)
.addGap(10, 20, 100)
.addComponent(button));
groupLayout.setVerticalGroup(
groupLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(clickMe)
.addComponent(button));
frame.pack();
frame.setVisible(true);
}
}
OR,
GroupLayout is a layout manager that hierarchically groups components in order to position them in a Container. GroupLayout works with the horizontal and vertical layouts separately. The layout is defined for each dimension independently. We do not need to worry about the vertical dimension when defining the horizontal layout, and vice versa, as the layout along each axis is totally independent of the layout along the other axis. When focusing on just one dimension, we only have to solve half the problem at one time. This is easier than handling both dimensions at once. This means that each component needs to be defined twice in the layout. If we forget to do this, GroupLayout will generate an exception. The following program demonstrates using GroupLayout.
import javax.swing.*;
public class GroupLayoutTest extends JFrame {
public GroupLayoutTest() {
JPanel panel = new JPanel();
GroupLayout layout = new GroupLayout(panel);
JButton one = new JButton("Button 1");
JButton two = new JButton("Button 2");
JButton three = new JButton("Button 3");
JButton four = new JButton("Button 4");
JButton five = new JButton("Button 5");
JButton six = new JButton("Button 6");
layout.setHorizontalGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(one)
.addComponent(two)
.addComponent(three)) //first group
.addGroup(layout.createSequentialGroup()
.addComponent(four)
.addComponent(five)) //second group
.addComponent(six));
layout.setVerticalGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(one).addComponent(two).addComponent(three))
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(four).addComponent(five))
.addComponent(six));
panel.setLayout(layout);
add(panel);
setSize(400, 400);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new GroupLayoutTest();
}
}
Comments
Post a Comment