SWING GroupLayout Class

Advertisements


Introduction

The class GroupLayout hierarchically groups components in order to position them in a Container .

Class declaration

Following is the declaration for javax.swing.GroupLayout class:

public class GroupLayout
   extends Object
      implements LayoutManager2

Field

Following are the fields for javax.swing.GroupLayout class:

  • static int DEFAULT_SIZE -- Indicates the size from the component or gap should be used for a particular range value.

  • static int PREFERRED_SIZE -- Indicates the preferred size from the component or gap should be used for a particular range value.

Class constructors

S.N.Constructor & Description
1GroupLayout(Container host)
Creates a GroupLayout for the specified Container.

Class methods

S.N.Method & Description
1void addLayoutComponent(Component component, Object constraints)
Notification that a Component has been added to the parent container.
2void addLayoutComponent(String name, Component component)
Notification that a Component has been added to the parent container.
3GroupLayout.ParallelGroup createBaselineGroup(boolean resizable, boolean anchorBaselineToTop)
Creates and returns a ParallelGroup that aligns it's elements along the baseline.
4GroupLayout.ParallelGroup createParallelGroup()
Creates and returns a ParallelGroup with an alignment of Alignment.LEADING.
5GroupLayout.ParallelGroup createParallelGroup(GroupLayout.Alignment alignment)
Creates and returns a ParallelGroup with the specified alignment.
6GroupLayout.ParallelGroup createParallelGroup(GroupLayout.Alignment alignment, boolean resizable)
Creates and returns a ParallelGroup with the specified alignment and resize behavior.
7GroupLayout.SequentialGroup createSequentialGroup()
Creates and returns a SequentialGroup.
8boolean getAutoCreateContainerGaps()
Returns true if gaps between the container and components that border the container are automatically created.
9boolean getAutoCreateGaps()
Returns true if gaps between components are automatically created.
10boolean getHonorsVisibility()
Returns whether component visiblity is considered when sizing and positioning components.
11float getLayoutAlignmentX(Container parent)
Returns the alignment along the x axis.
12float getLayoutAlignmentY(Container parent)
Returns the alignment along the y axis.
13LayoutStyle getLayoutStyle()
Returns the LayoutStyle used for calculating the preferred gap between components.
14void invalidateLayout(Container parent)
Invalidates the layout, indicating that if the layout manager has cached information it should be discarded.
15void layoutContainer(Container parent)
Lays out the specified container.
16void linkSize(Component... components)
Forces the specified components to have the same size regardless of their preferred, minimum or maximum sizes.
17void linkSize(int axis, Component... components)
Forces the specified components to have the same size along the specified axis regardless of their preferred, minimum or maximum sizes.
18Dimension maximumLayoutSize(Container parent)
Returns the maximum size for the specified container.
19Dimension minimumLayoutSize(Container parent)
Returns the minimum size for the specified container.
20Dimension preferredLayoutSize(Container parent)
Returns the preferred size for the specified container.
21void removeLayoutComponent(Component component)
Notification that a Component has been removed from the parent container.
22void replace(Component existingComponent, Component newComponent)
Replaces an existing component with a new one.
23void setAutoCreateContainerGaps(boolean autoCreateContainerPadding)
Sets whether a gap between the container and components that touch the border of the container should automatically be created.
24void setAutoCreateGaps(boolean autoCreatePadding)
Sets whether a gap between components should automatically be created.
25void setHonorsVisibility(boolean honorsVisibility)
Sets whether component visiblity is considered when sizing and positioning components.
26void setHonorsVisibility(Component component, Boolean honorsVisibility)
Sets whether the component's visiblity is considered for sizing and positioning.
27void setHorizontalGroup(GroupLayout.Group group)
Sets the Group that positions and sizes components along the horizontal axis.
28void setLayoutStyle(LayoutStyle layoutStyle)
Sets the LayoutStyle used to calculate the preferred gaps between components.
29void setVerticalGroup(GroupLayout.Group group)
Sets the Group that positions and sizes components along the vertical axis.
30String toString()
Returns a string representation of this GroupLayout.

Methods inherited

This class inherits methods from the following classes:

  • java.lang.Object

GroupLayout Example

Create the following java program using any editor of your choice in say D:/ > SWING > com > tutorialspoint > gui >

SwingLayoutDemo.java
package com.tutorialspoint.gui;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class SwingLayoutDemo {
   private JFrame mainFrame;
   private JLabel headerLabel;
   private JLabel statusLabel;
   private JPanel controlPanel;
   private JLabel msglabel;

   public SwingLayoutDemo(){
      prepareGUI();
   }

   public static void main(String[] args){
      SwingLayoutDemo swingLayoutDemo = new SwingLayoutDemo();  
      swingLayoutDemo.showGroupLayoutDemo();       
   }
      
   private void prepareGUI(){
      mainFrame = new JFrame("Java SWING Examples");
      mainFrame.setSize(400,400);
      mainFrame.setLayout(new GridLayout(3, 1));

      headerLabel = new JLabel("",JLabel.CENTER );
      statusLabel = new JLabel("",JLabel.CENTER);        

      statusLabel.setSize(350,100);
      mainFrame.addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent windowEvent){
	        System.exit(0);
         }        
      });    
      controlPanel = new JPanel();
      controlPanel.setLayout(new FlowLayout());

      mainFrame.add(headerLabel);
      mainFrame.add(controlPanel);
      mainFrame.add(statusLabel);
      mainFrame.setVisible(true);  
   }

   private void showGroupLayoutDemo(){
      headerLabel.setText("Layout in action: GroupLayout");      

      JPanel panel = new JPanel();
      // panel.setBackground(Color.darkGray);
      panel.setSize(200,200);
      GroupLayout layout = new GroupLayout(panel);
      layout.setAutoCreateGaps(true);
      layout.setAutoCreateContainerGaps(true);
      JButton btn1 = new JButton("Button 1");
      JButton btn2 = new JButton("Button 2");
      JButton btn3 = new JButton("Button 3");

      layout.setHorizontalGroup(layout.createSequentialGroup()
         .addComponent(btn1)
         .addGroup(layout.createSequentialGroup()
            .addGroup(layout.createParallelGroup(
               GroupLayout.Alignment.LEADING)
               .addComponent(btn2)
               .addComponent(btn3)       
            )
         )    
      );


      layout.setVerticalGroup(layout.createSequentialGroup()
         .addComponent(btn1)
         .addComponent(btn2)
         .addComponent(btn3)                                    
      );
      panel.setLayout(layout);        
      controlPanel.add(panel);

      mainFrame.setVisible(true);  
   }
}

Compile the program using command prompt. Go to D:/ > SWING and type the following command.

D:\SWING>javac com\tutorialspoint\gui\SwingLayoutDemo.java

If no error comes that means compilation is successful. Run the program using following command.

D:\SWING>java com.tutorialspoint.gui.SwingLayoutDemo

Verify the following output

SWING GroupLayout

Advertisements
Advertisements