Java Swing | GroupLayout Class
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): Notifiy 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.
- 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.
Note: The above programs might not run in an online IDE. Please use an offline compiler.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.