Advanced Java Programming - Old Questions

2. What is layout management? Discuss any three layout management classes with example of each.

10 marks | Asked in 2072

The layout management is 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. The layout management classes are given below:

1. Border Layout

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.

Components of the BorderLayout Manager

·         BorderLayout.NORTH

·         BorderLayout.SOUTH

·         BorderLayout..EAST

·         BorderLayout.WEST

·         BorderLayout.CENTER

BorderLayout Constructors:

BorderLayout()

creates a border layout but with no gaps between the components.

BorderLayout(int hgap, int vgap)

creates a border layout with the given horizontal and vertical gaps between the components.


Example:

import java.awt.BorderLayout;  

import javax.swing.JButton;

import javax.swing.JFrame;

  

public class BorderLayoutExample {  

BorderLayoutExample(){  

    JFrame f=new JFrame();  

      

    JButton b1=new JButton("NORTH");;  

    JButton b2=new JButton("SOUTH");;  

    JButton b3=new JButton("EAST");;  

    JButton b4=new JButton("WEST");;  

    JButton b5=new JButton("CENTER");;  

      

    f.add(b1,BorderLayout.NORTH);  

    f.add(b2,BorderLayout.SOUTH);  

    f.add(b3,BorderLayout.EAST);  

    f.add(b4,BorderLayout.WEST);  

    f.add(b5,BorderLayout.CENTER);  

      

    f.setSize(300,300);  

    f.setVisible(true);  

}  

public static void main(String[] args) {  

    new BorderLayoutExample();  

}  

}  


2. Flow Layout


The FlowLayout is used to arrange the components in a line, one after another (in a flow).  It arranges components in a line, if no space left remaining components goes to next line. Align property determines alignment of the components as left, right, center etc.

FlowLayout Constructors:

FlowLayout()

 

creates a flow layout with centered alignment and a default 5 unit horizontal and vertical gap.

FlowLayout(int align)

creates a flow layout with the given alignment and a default 5 unit horizontal and vertical gap.

FlowLayout(int align, inthgap, intvgap)

creates a flow layout with the given alignment and the given horizontal and vertical gap.

 

Example:     

import java.awt.FlowLayout;

import javax.swing.JButton;

import javax.swing.JFrame;

public class FlowLayoutExample {

 FlowLayoutExample(){

     JFrame f = new JFrame("Flow Layout");

        JButton b1 = new JButton("button 1");

        JButton b2 = new JButton("button 2");

        JButton b3 = new JButton("button 3");

        JButton b4 = new JButton("button 4");

        JButton b5 = new JButton("button 5");

        f.add(b1);

        f.add(b2);

        f.add(b3);

        f.add(b4);

        f.add(b5);

        f.setLayout(new FlowLayout());

        f.setSize(300,300);  

        f.setVisible(true);  

    }

    public static void main(String[] args) {

        new FlowLayoutExample();

    }

}


3. Grid Layout

The GridLayout is used to arrange the components in rectangular grid. One component is displayed in each rectangle. Components are placed in columns and rows.

Every Component in a GridLayout has the same width and height. Components are added to a GridLayout starting at the top-left cell of the grid and proceeding left to right until the row is full. Then the process continues left to right on the next row of the grid, and so on.

GridLayout Constructors:

GridLayout()

creates a grid layout with one column per component in a row.

GridLayout(int rows, int cols)

creates a grid layout with the given rows and columns but no gaps between the components.

GridLayout(int rows, int cols, int int hgap, int vgap)

creates a grid layout with the given rows and columns alongwith given horizontal and vertical gaps.

 

Example:

import java.awt.*;

import javax.swing.*;

public class GridDemo

{

      JButton b1, b2, b3, b4, b5, b6;

      GridDemo()

      {

           JFrame f = new JFrame("GridLayoutDemo");

           b1 = new JButton("A");

           b2 = new JButton("B");

           b3 = new JButton("C");

           b4 = new JButton("D");

           b5 = new JButton("E");

           b6 = new JButton("F");

           f.add(b1);

           f.add(b2);

           f.add(b3);

           f.add(b4);

           f.add(b5);

           f.add(b6);

           f.setLayout(new GridLayout(2, 3));

           f.setSize(200, 200);

           f.setVisible(true);

      }

      public static void main(String args[])

      {

           new GridDemo();

      }

}