SWING - GROUPLAYOUT CLASS http://www.tutorialspoint.com/swing/swing_grouplayout.htm Copyright tutorialspoint.com 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 1 GroupLayoutContainerhost Creates a GroupLayout for the specified Container. Class methods S.N. Method & Description 1 void addlayoutcomponentcomponentcomponent, Objectconstraints Notification that a Component has been added to the parent container. 2 void addlayoutcomponentstringname, Componentcomponent Notification that a Component has been added to the parent container. 3 GroupLayout.ParallelGroup createbaselinegroup booleanresizable, booleananchorbaselinetotop Creates and returns a ParallelGroup that aligns it's elements along the baseline. 4 GroupLayout.ParallelGroup createparallelgroup Creates and returns a ParallelGroup with an alignment of Alignment.LEADING.
5 GroupLayout.ParallelGroup createparallelgroupgrouplayout. Alignmentalignment Creates and returns a ParallelGroup with the specified alignment. 6 GroupLayout.ParallelGroup createparallelgroup GroupLayout. Alignmentalignment, booleanresizable Creates and returns a ParallelGroup with the specified alignment and resize behavior. 7 GroupLayout.SequentialGroup createsequentialgroup Creates and returns a SequentialGroup. 8 boolean getautocreatecontainergaps Returns true if gaps between the container and components that border the container are automatically created. 9 boolean getautocreategaps Returns true if gaps between components are automatically created. 10 boolean gethonorsvisibility Returns whether component visiblity is considered when sizing and positioning components. 11 float getlayoutalignmentxcontainerparent Returns the alignment along the x axis. 12 float getlayoutalignmentycontainerparent Returns the alignment along the y axis. 13 LayoutStyle getlayoutstyle Returns the LayoutStyle used for calculating the preferred gap between components. 14 void invalidatelayoutcontainerparent Invalidates the layout, indicating that if the layout manager has cached information it should be discarded. 15 void layoutcontainercontainerparent Lays out the specified container. 16 void linksizecomponent... components Forces the specified components to have the same size regardless of their preferred, minimum or maximum sizes. 17 void linksizeintaxis, Component... components Forces the specified components to have the same size along the specified axis regardless of their preferred, minimum or maximum sizes.
18 Dimension maximumlayoutsizecontainerparent Returns the maximum size for the specified container. 19 Dimension minimumlayoutsizecontainerparent Returns the minimum size for the specified container. 20 Dimension preferredlayoutsizecontainerparent Returns the preferred size for the specified container. 21 void removelayoutcomponentcomponentcomponent Notification that a Component has been removed from the parent container. 22 void replacecomponentexistingcomponent, ComponentnewComponent Replaces an existing component with a new one. 23 void setautocreatecontainergapsbooleanautocreatecontainerpadding Sets whether a gap between the container and components that touch the border of the container should automatically be created. 24 void setautocreategapsbooleanautocreatepadding Sets whether a gap between components should automatically be created. 25 void sethonorsvisibilitybooleanhonorsvisibility Sets whether component visiblity is considered when sizing and positioning components. 26 void sethonorsvisibilitycomponentcomponent, BooleanhonorsVisibility Sets whether the component's visiblity is considered for sizing and positioning. 27 void sethorizontalgroupgrouplayout. Groupgroup Sets the Group that positions and sizes components along the horizontal axis. 28 void setlayoutstylelayoutstylelayoutstyle Sets the LayoutStyle used to calculate the preferred gaps between components. 29 void setverticalgroupgrouplayout. Groupgroup Sets the Group that positions and sizes components along the vertical axis. 30 String 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 Loading [MathJax]/jax/output/HTML-CSS/fonts/TeX/fontdata.js