Monday 26 March 2012

Layouts and Layout Managers in Java

Layout Manager:

•All of the components we have seen until now have been positioned by default by a default layout manager.
•A layout manager arranges the GUI components in a certain order.
•If we don’t use any layout manager, then we have to arrange the components manually by using “setBounds()” method and other methods available in the “Component” class.
•We prefer using layout managers over arranging the components manually due to two reasons:
1)It takes huge amount of time to arrange a large number of components.
2)In some cases the width and height information is not known before hand.
•Every “Container” object has a layout manager associated with it.
•A layout manager is an instance of a class which implements the “LayoutManager” interface.
•The layout manager is set by the “setLayout()” method.
•The syntax of setLayout() method is as shown below:
void setLayout(LayoutManager layoutObj)
•If you want to arrange the components manually you can pass “null” in place of “layoutObj”.


Layout Manager Types:

•Java supports the following layout manager classes:
1.FlowLayout
2.BorderLayout
3.GridLayout
4.CardLayout
5.GridBagLayout
6.BoxLayout
7.GroupLayout
8.SpringLayout

FlowLayout:

•FlowLayout is the default layout.
•In flow layout, components are arranged left to right and top to bottom by default.
•A 5 pixels of space is given above and below and left and right of the component by default in flow layout.
•FlowLayout class provides the following constructors:
1.FlowLayout( )
2.FlowLayout(int how)
3.FlowLayout(int how, int horz, int vert)


BorderLayout:

•The BorderLayout divides the entire region into five regions. Four of them are narrow fixed width regions namely: north, south, east and west. The remaining region is referred to as center.
•BorderLayout class provides the following constructors:
1.BorderLayout( )
2.BorderLayout(int horz, int vert)
•BorderLayout class provides the following constants to specify the regions:
1.BorderLayout.NORTH
2.BorderLayout.SOUTH
3.BorderLayout.EAST
4.BorderLayout.WEST
5.BorderLayout.CENTER
 

GridLayout:

•GridLayout allows the components to be arranged in a two dimensional grid.
•While applying the Gridlayout we have to specify the number of rows and columns in the grid.
•GridLayout class supports the following constructors:
1.GridLayout( )
2.GridLayout(int numRows, int numColumns)
3.GridLayout(int numRows, int numColumns, int horz, int vert)


CardLayout:

•CardLayout is a unique layout manager class among all the layout managers.
•This layout manages various container objects(generally panels) as a deck of cards.
•At anytime we can see one container and all other containers are hidden.
•We must provide code to show only one container at a time and hide all other containers.
•CardLayout class provides the following constructors:
1.CardLayout( )
2.CardLayout(int horz, int vert)


GridBagLayout:

•The GridBagLayout can be used to divide the entire display area into a grid having user defined number of columns.
•The number of columns in each grid(row) can vary.
•The components in a cell can be aligned as per the users choice.
•Though GridBagLayout is complicated than other layout managers, it provides extended functionality over the others.
•GridBagLayout class provides the following constructor:
1.GridBagLayout( )

•We can set various constraints over the components in a cell by creating an object for “GridBagConstraints” class.
•Following are the some of the fields in “GridBagConstraints” class:

No comments:

Post a Comment