Java LayoutManagers
Java LayoutManagers
Java LayoutManagers
The LayoutManagers are used to arrange components in a particular manner.
The Java LayoutManagers facilitates us to control the positioning and size of the
components in GUI forms.
LayoutManager is an interface that is implemented by all the classes of layout managers.
There are the following classes that represent the layout managers:
1. java.awt.BorderLayout
2. java.awt.FlowLayout
3. java.awt.GridLayout
4. java.awt.CardLayout
5. java.awt.GridBagLayout
6. javax.swing.BoxLayout
7. javax.swing.GroupLayout
8. javax.swing.ScrollPaneLayout
9. javax.swing.SpringLayout etc.
Java BorderLayout
Dr Bharti Sharma
The BorderLayout is used to arrange the components in five regions: north, south, east, west, and
center.
Each region (area) may contain one component only.
It is the default layout of a frame or window.
The BorderLayout provides five constants for each region:
1. public static final int NORTH
2. public static final int SOUTH
3. public static final int EAST
4. public static final int WEST
5. public static final int CENTER
Constructors of BorderLayout class:
o BorderLayout(): creates a border layout but with no gaps between the components.
o BorderLayout(int hgap, int vgap): creates a border layout with the given horizontal and
vertical gaps between the components.
Example of BorderLayout class: Using BorderLayout() constructor
Dr Bharti Sharma
FileName: Border.java
1. import java.awt.*;
2. import javax.swing.*;
3.
4. public class Border
5. {
6. JFrame f;
7. Border()
8. {
9. f = new JFrame();
10.
11. // creating buttons
12. JButton b1 = new JButton("NORTH"); // the button will be labeled as NORTH
13. JButton b2 = new JButton("SOUTH"); // the button will be labeled as SOUTH
14. JButton b3 = new JButton("EAST"); // the button will be labeled as EAST
15. JButton b4 = new JButton("WEST"); // the button will be labeled as WEST
16. JButton b5 = new JButton("CENTER"); // the button will be labeled as CENTER
17.
18. f.add(b1, BorderLayout.NORTH); // b1 will be placed in the North Direction
19. f.add(b2, BorderLayout.SOUTH); // b2 will be placed in the South Direction
20. f.add(b3, BorderLayout.EAST); // b2 will be placed in the East Direction
Dr Bharti Sharma
2. import java.awt.*;
3. import javax.swing.*;
4. public class BorderLayoutExample
5. {
6. JFrame jframe;
7. // constructor
8. BorderLayoutExample()
9. {
10. // creating a Frame
11. jframe = new JFrame();
12. // create buttons
13. JButton btn1 = new JButton("NORTH");
14. JButton btn2 = new JButton("SOUTH");
15. JButton btn3 = new JButton("EAST");
16. JButton btn4 = new JButton("WEST");
17. JButton btn5 = new JButton("CENTER");
18. // creating an object of the BorderLayout class using
19. // the parameterized constructor where the horizontal gap is 20
20. // and vertical gap is 15. The gap will be evident when buttons are placed
21. // in the frame
22. jframe.setLayout(new BorderLayout(20, 15));
Dr Bharti Sharma
In such a case, only the latest component added is shown in the frame, and all the components
added previously get discarded.
The latest component covers the whole area. The following example shows the same.
FileName: BorderLayoutWithoutRegionExample.java
1. // import statements
2. import java.awt.*;
3. import javax.swing.*;
4.
5. public class BorderLayoutWithoutRegionExample
6. {
7. JFrame jframe;
8.
9. // constructor
10. BorderLayoutWithoutRegionExample()
11. {
12. jframe = new JFrame();
13.
14. JButton btn1 = new JButton("NORTH");
15. JButton btn2 = new JButton("SOUTH");
Dr Bharti Sharma
37.
38. // main method
39. public static void main(String argvs[])
40. {
41. new BorderLayoutWithoutRegionExample();
42. }
43. }
Output:
Dr Bharti Sharma
Dr Bharti Sharma
Java FlowLayout
The Java FlowLayout class is used to arrange the components in a line, one after another (in a
flow). It is the default layout of the applet or panel.
Fields of FlowLayout class
1. public static final int LEFT
2. public static final int RIGHT
3. public static final int CENTER
4. public static final int LEADING
5. public static final int TRAILING
Constructors of FlowLayout class
1. FlowLayout(): creates a flow layout with centered alignment and a default 5 unit horizontal
and vertical gap.
2. FlowLayout(int align): creates a flow layout with the given alignment and a default 5 unit
horizontal and vertical gap.
Dr Bharti Sharma
3. FlowLayout(int align, int hgap, int vgap): creates a flow layout with the given alignment
and the given horizontal and vertical gap.
Example of FlowLayout class: Using FlowLayout() constructor
FileName: FlowLayoutExample.java
1. // import statements
2. import java.awt.*;
3. import javax.swing.*;
4.
5. public class FlowLayoutExample
6. {
7.
8. JFrame frameObj;
9.
10. // constructor
11. FlowLayoutExample()
12. {
13. // creating a frame object
14. frameObj = new JFrame();
15.
Dr Bharti Sharma
Example of FlowLayout class: Using FlowLayout(int align, int hgap, int vgap) constructor
Dr Bharti Sharma
FileName: FlowLayoutExample1.java
1. // import statement
2. import java.awt.*;
3. import javax.swing.*;
4.
5. public class FlowLayoutExample1
6. {
7. JFrame frameObj;
8.
9. // constructor
10. FlowLayoutExample1()
11. {
12. // creating a frame object
13. frameObj = new JFrame();
14.
15. // creating the buttons
16. JButton b1 = new JButton("1");
17. JButton b2 = new JButton("2");
18. JButton b3 = new JButton("3");
19. JButton b4 = new JButton("4");
20. JButton b5 = new JButton("5");
Dr Bharti Sharma
Java GridLayout
The Java GridLayout class is used to arrange the components in a rectangular grid. One component
is displayed in each rectangle.
Dr Bharti Sharma
9. // constructor
10. GridLayoutExample()
11. {
12. frameObj = new JFrame();
13.
14. // creating 9 buttons
15. JButton btn1 = new JButton("1");
16. JButton btn2 = new JButton("2");
17. JButton btn3 = new JButton("3");
18. JButton btn4 = new JButton("4");
19. JButton btn5 = new JButton("5");
20. JButton btn6 = new JButton("6");
21. JButton btn7 = new JButton("7");
22. JButton btn8 = new JButton("8");
23. JButton btn9 = new JButton("9");
24.
25. // adding buttons to the frame
26. // since, we are using the parameterless constructor, therfore;
27. // the number of columns is equal to the number of buttons we
28. // are adding to the frame. The row count remains one.
29. frameObj.add(btn1); frameObj.add(btn2); frameObj.add(btn3);
Dr Bharti Sharma
4. JFrame f;
5. MyGridLayout(){
6. f=new JFrame();
7. JButton b1=new JButton("1");
8. JButton b2=new JButton("2");
9. JButton b3=new JButton("3");
10. JButton b4=new JButton("4");
11. JButton b5=new JButton("5");
12. JButton b6=new JButton("6");
13. JButton b7=new JButton("7");
14. JButton b8=new JButton("8");
15. JButton b9=new JButton("9");
16. // adding buttons to the frame
17. f.add(b1); f.add(b2); f.add(b3);
18. f.add(b4); f.add(b5); f.add(b6);
19. f.add(b7); f.add(b8); f.add(b9);
20.
21. // setting grid layout of 3 rows and 3 columns
22. f.setLayout(new GridLayout(3,3));
23. f.setSize(300,300);
24. f.setVisible(true);
Dr Bharti Sharma
25. }
26. public static void main(String[] args) {
27. new MyGridLayout();
28. }
29. }
Output:
Example of GridLayout class: Using GridLayout(int rows, int columns, int hgap, int vgap)
Constructor
The following example inserts horizontal and vertical gaps between buttons using the
parameterized constructor GridLayout(int rows, int columns, int hgap, int vgap).
FileName: GridLayoutExample1.java
1. // import statements
2. import java.awt.*;
3. import javax.swing.*;
4.
5. public class GridLayoutExample1
6. {
7.
8. JFrame frameObj;
9.
10. // constructor
11. GridLayoutExample1()
12. {
13. frameObj = new JFrame();
14.
Dr Bharti Sharma
Java CardLayout
The Java CardLayout class manages the components in such a manner that only one component
is visible at a time. It treats each component as a card that is why it is known as CardLayout.
Constructors of CardLayout Class
Dr Bharti Sharma
1. CardLayout(): creates a card layout with zero horizontal and vertical gap.
2. CardLayout(int hgap, int vgap): creates a card layout with the given horizontal and vertical
gap.
Commonly Used Methods of CardLayout Class
o public void next(Container parent): is used to flip to the next card of the given container.
o public void previous(Container parent): is used to flip to the previous card of the given
container.
o public void first(Container parent): is used to flip to the first card of the given container.
o public void last(Container parent): is used to flip to the last card of the given container.
o public void show(Container parent, String name): is used to flip to the specified card with
the given name.
Example of CardLayout Class: Using Default Constructor
The following program uses the next() method to move to the next card of the container.
FileName: CardLayoutExample1.java
1. // import statements
2. import java.awt.*;
3. import javax.swing.*;
Dr Bharti Sharma
4. import java.awt.event.*;
5.
6. public class CardLayoutExample1 extends JFrame implements ActionListener
7. {
8.
9. CardLayout crd;
10.
11. // button variables to hold the references of buttons
12. JButton btn1, btn2, btn3;
13. Container cPane;
14.
15. // constructor of the class
16. CardLayoutExample1()
17. {
18.
19. cPane = getContentPane();
20.
21. //default constructor used
22. // therefore, components will
23. // cover the whole area
24. crd = new CardLayout();
Dr Bharti Sharma
25.
26. cPane.setLayout(crd);
27.
28. // creating the buttons
29. btn1 = new JButton("Apple");
30. btn2 = new JButton("Boy");
31. btn3 = new JButton("Cat");
32.
33. // adding listeners to it
34. btn1.addActionListener(this);
35. btn2.addActionListener(this);
36. btn3.addActionListener(this);
37.
38. cPane.add("a", btn1); // first card is the button btn1
39. cPane.add("b", btn2); // first card is the button btn2
40. cPane.add("c", btn3); // first card is the button btn3
41.
42. }
43. public void actionPerformed(ActionEvent e)
44. {
45. // Upon clicking the button, the next card of the container is shown
Dr Bharti Sharma
46. // after the last card, again, the first card of the container is shown upon clicking
47. crd.next(cPane);
48. }
49.
50. // main method
51. public static void main(String argvs[])
52. {
53. // creating an object of the class CardLayoutExample1
54. CardLayoutExample1 crdl = new CardLayoutExample1();
55.
56. // size is 300 * 300
57. crdl.setSize(300, 300);
58. crdl.setVisible(true);
59. crdl.setDefaultCloseOperation(EXIT_ON_CLOSE);
60. }
61. }
Output:
Dr Bharti Sharma
Again, we reach the first card of the container if the cat button is clicked, and the cycle continues.
Dr Bharti Sharma
5.
6. public class CardLayoutExample2 extends JFrame implements ActionListener{
7. CardLayout card;
8. JButton b1,b2,b3;
9. Container c;
10. CardLayoutExample2(){
11.
12. c=getContentPane();
13. card=new CardLayout(40,30);
14. //create CardLayout object with 40 hor space and 30 ver space
15. c.setLayout(card);
16.
17. b1=new JButton("Apple");
18. b2=new JButton("Boy");
19. b3=new JButton("Cat");
20. b1.addActionListener(this);
21. b2.addActionListener(this);
22. b3.addActionListener(this);
23.
24. c.add("a",b1);c.add("b",b2);c.add("c",b3);
25.
Dr Bharti Sharma
26. }
27. public void actionPerformed(ActionEvent e) {
28. card.next(c);
29. }
30.
31. public static void main(String[] args) {
32. CardLayoutExample2 cl=new CardLayoutExample2();
33. cl.setSize(400,400);
34. cl.setVisible(true);
35. cl.setDefaultCloseOperation(EXIT_ON_CLOSE);
36. }
37. }
download this example
Output:
Dr Bharti Sharma
1. // Import statements.
2. import java.awt.*;
3. import java.awt.event.ActionEvent;
4. import java.awt.event.ActionListener;
5. import javax.swing.*;
6.
7. public class CardLayoutExample3 extends JFrame
8. {
9.
10. // Initializing the value of
11. // currCard to 1 .
12. private int currCard = 1;
13.
14. // Declaring of objects
15. // of the CardLayout class.
16. private CardLayout cObjl;
17.
18. // constructor of the class
19. public CardLayoutExample3()
20. {
21.
Dr Bharti Sharma
127. {
128. public void actionPerformed(ActionEvent ae)
129. {
130.
131. // using the first cObjl CardLayout
132. cObjl.first(cPanel);
133.
134. // value of currCard is 1
135. currCard = 1;
136. }
137. });
138.
139. // add lastButton in ActionListener
140. lastButton.addActionListener(new ActionListener()
141. {
142. public void actionPerformed(ActionEvent ae)
143. {
144.
145. // using the last cObjl CardLayout
146. cObjl.last(cPanel);
147.
Dr Bharti Sharma
169. });
170.
171. // add previousButton in ActionListener
172. previousButton.addActionListener(new ActionListener()
173. {
174. public void actionPerformed(ActionEvent ae)
175. {
176.
177. if (currCard > 1)
178. {
179.
180. // decrease the value
181. // of currCard by 1
182. currCard = currCard - 1;
183.
184. // show the value of currCard
185. cObjl.show(cPanel, "" + (currCard));
186. }
187. }
188. });
189.
Dr Bharti Sharma
Output: