Java Layout Managers


 

Layout Managers

§  When in any container like Japplet or Jframe contains more number of components, it is important to arrange all components in an attractive way.

§  To make it easy layout manager can be used.

§  A layout manager is an object that controls the size and position of components inside the container.

§  For example a window is a container that includes components such as buttons and labels. The layout manager that you assign to the window determines how the components are sized and positioned within the window.

§  Layout managers are interface classes.

§  Each layout manager defines methods that arrange components within a container, and each component within container you place can be container.

§  So it is possible to write container within container.

 

Layout options

§  BorderLayout

§  BoxLayout

§  CardLayout

§  FlowLayout

§  GridBagLayout

§  GridLayout

 

 

Border Layout

§  Every content pane is initialized to use a BorderLayout.

§  Border layout is the default manager class for all content pane.

§   A BorderLayout places components in up to five areas: top, bottom, left, right, and center.

§   All extra space is placed in the center area.

§  The border layout class provides five named constants for the regions – BorderLayout.NORTH,.SOUTH,.EAST,.WEST,.CENTER

§  When you place exactly five components in a container and use borderlayout, each components fills one entire region when the application runs.

§  Java determines the exact size of each components based on the components contents.

§  If you drag the container’s border to make it wider the north, south and center regions become wider, but the east and west regions do not change.

§  If you increase the containers’ height the east and west, center region becomes taller, but the north and south regions do not change.

§  When you create Container name con, you can set its layout manager to BorderLayout with the following:

§  Con.setLayout(new BorderLayout());

§  Or in Japplet you can write the following:

§  setLayout(new BorderLayout());

§  This.setLayout(new BorderLayout());

 

import javax.swing.*;

import java.awt.*;

/*<object code=bl.class width=200 height=200></object> */

public class bl extends JApplet

{

   Container con= getContentPane();

   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");

   public void init()

   {

con.setLayout(new BorderLayout());

con.add(b1, BorderLayout.NORTH);

con.add(b2, BorderLayout.SOUTH);

con.add(b3, BorderLayout.EAST);

con.add(b4, BorderLayout.WEST);

con.add(b5, BorderLayout.CENTER);

    }

  }

Flow Layout

§  The Flow Layout class puts components in a row, sized at their preferred size.

§  If the horizontal space in the container is too small to put all the components in one row, the FlowLayout class uses multiple rows.

§   If the container is wider than necessary for a row of components, the row is, by default, centered horizontally within the container.

§  To specify that the row is to aligned either to the left or right, use a FlowLayout constructor that takes an alignment argument.

§  Another constructor of the FlowLayout class specifies how much vertical or horizontal padding is put around the components.

§  When you use borderlayout and then resize the window, the components change size accordingly because their regions change.

§  When you use BorderLayout and then resize the window, the components change size accordingly because their region change.

§  When you use flowlayout and then resize the window, each component retains its size, but it might become partially changed.

§  The flowlayout class contains three constants you can use to align components with a container:

§  FlowLayout.LEFT

§  FlowLayout.CENTER

§  FlowLayout.RIGHT

 

import javax.swing.*;

import java.awt.*;

/*<object code=fl.class width=200 height=200></object> */

public class fl extends JApplet

{

   Container con= getContentPane();

   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");

   public void init()

   {

con.setLayout(new FlowLayout());

con.add(b1);

con.add(b2);

con.add(b3);

con.add(b4);

con.add(b5);

    }

  }

GridLayout

§  If you want to use components into equal rows and columns, you can use the GridLayout manager class.

§  When you create a GridLayout object, you indicate the number of rows and columns you want, and then the container surface is divided into a grid, much like a screen you see when using a spreadsheet program.

§  Con.setLayout(new GridLayout(4,5)) will set the anonymous grid layout with four horizontal line and 5 vertical line.

§  Similarly, within Japplet you can establish the same layout with either the following:

§  This.setLayout(new GridLayout(4,5));

§  setLayout(new GridLayout(4,5))

import javax.swing.*;

import java.awt.*;

 

/*<object code=gl.class width=200 height=200></object> */

public class gl extends JApplet

{

   Container con= getContentPane();

   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");

   public void init()

   {

con.setLayout(new GridLayout(2,3,5,5));

con.add(b1);

con.add(b2);

con.add(b3);

con.add(b4);

con.add(b5);

    }

  }

 

Card Layout

§  each component that a CardLayout manages is like a playing card or trading card in a stack, where only the top card is visible at any time. You can choose the card that is showing in any of the following ways:

§  By asking for either the first or last card, in the order it was added to the container

§  By flipping through the deck backwards or forwards

§  By specifying a card with a specific name

§  CardLayout() A card layout is created from the card layout without a horizontal or vartical gap.

§  CardLayout(int hgap, int vgap) creates a card layout with the horizontal and vertical gaps.

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

 

/*<object code=cl.class width=200 height=200></object> */

 

public class cl extends JApplet implements ActionListener

{

   Container con= getContentPane();

CardLayout cards = new CardLayout();

 JButton b1=new JButton("north");

JButton b2=new JButton("south");

JButton b3=new JButton("east");

   public void init()

   {

setLayout(cards);

con.add(" ",b1);

b1.addActionListener(this);

con.add(" ",b2);

b2.addActionListener(this);

con.add(" ",b3);

b3.addActionListener(this);

 

   }

public void actionPerformed(ActionEvent e)

{

cards.next(getContentPane());   

}

  }