Sunday, February 12, 2012

Using layout managers

The type of layout manager that you use depends largely on your display requirements.
There are five original layout managers commonly used. These are
  • FlowLayout
  • GridLayout
  • GridBagLayout
  • BorderLayout
  • CardLayout
FlowLayout
The FlowLayout class provides the simplest means of laying out components. It is the default layout for certain Containers. The Panel employs the FlowLayout as its default manager.

FlowLayout inserts components in a row – from left to right – as you add them to the container. When a component won't fit at the end of a row, it's wrapped to the start of a new row.

An example of the code for FlowLayout is

import java.awt.*;
import java.applet.Applet;

public class DemoFlowLayout extends Applet {
  public void init () {
    setLayout (new FlowLayout ());
    Button b1 = new Button("one");
    Button b2 = new Button("two");
    Button b3 = new Button("three");
    Button b4 = new Button("four");
    Button b5 = new Button("five");
    add(b1);
    add(b2);
    add(b3);
    add(b4);
    add(b5);
  }
}


The default alignment of the FlowLayout class is centered. However, you can change this to left or right alignment by passing the constants FlowLayout.LEFT or FlowLayout.RIGHT to the FlowLayout constructor. For example, the code for left alignment is

setLayout(new FlowLayout(FlowLayout.LEFT));

An example of a flow layout that is aligned to the left is

import java.awt.*;
import java.applet.Applet;

public class DemoFlowLayout extends Applet {
  public void init () {
    setLayout(new FlowLayout(FlowLayout.LEFT));
    Button b1 = new Button("one");
    Button b2 = new Button("two");
    Button b3 = new Button("three");
    Button b4 = new Button("four");
    Button b5 = new Button("five");
    add(b1);
    add(b2);
    add(b3);
    add(b4);
    add(b5);
  }
}


FlowLayout creates a default gap of 5 pixels between components. However, you can change both the horizontal and vertical gap between components by passing appropriate arguments to the FlowLayout constructor.

For instance, the code example specifies a horizontal gap of 10 pixels and a vertical gap of 4 pixels.

setLayout (new FlowLayout (FlowLayout.CENTER,10,4));

Another complete example of the code for FlowLayout follows.

import java.awt.*;
import java.applet.Applet;

public class NewApplet extends Applet {
  public void init () {
    setLayout (new FlowLayout (FlowLayout.CENTER,10,4));
    Label 11 = new Label ("First Name: ");
    TextField t1 = new TextField (12);
    Label 12 = new Label ("Last Name: ");
    TextField t2 = new TextField (12);
    add(l1);
    add(t1);
    add(l2);
    add(t2);
  }
}


FlowLayout is best suited to simple layouts where components need to be displayed at their natural size. For a large number of components, you should use another layout manager.
GridLayout
You can use the GridLayout class to make a number of components of the same size. The GridLayout layout manager divides a panel into a grid of uniform cells. It then adds components one at time, building up cell rows and columns from left to right, top to bottom.

The code to implement this GridLayout is

import java.awt.*;
import java.applet.Applet;

public class DemoGridLayout extends Applet {
  public void init () {
    Panel p1 = new Panel ();
    //set layout for panel p1
    p1.setLayout (new GridLayout(2,3));
    Button b1 = new Button("one");
    Button b2 = new Button("two");
    Button b3 = new Button("three");
    Button b4 = new Button("four");
    Button b5 = new Button("five");
    p1.add(b1);
    p1.add(b2);
    p1.add(b3);
    p1.add(b4);
    p1.add(b5);
  }
}


You set the number of rows and columns for a particular panel in the constructor of its GridLayout object. Consider the code that sets the GridLayout with two rows and three columns.

p1.setLayout (new GridLayout(2,3));
Suppose you decide to add the buttons in sequence. The first three buttons fill the first row of a two-row by three-column grid. Then the fourth and fifth buttons are added in the second row.

Here is the code again.

import java.awt.*;
import java.applet.Applet;

public class DemoGridLayout extends Applet {
  public void init () {
    Panel p1 = new Panel ();
    //set layout for panel p1
    p1.setLayout (new GridLayout(2,3));
    Button b1 = new Button("one");
    Button b2 = new Button("two");
    Button b3 = new Button("three");
    Button b4 = new Button("four");
    Button b5 = new Button("five");
    p1.add(b1);
    p1.add(b2);
    p1.add(b3);
    p1.add(b4);
    p1.add(b5);
  }
}


The Gridlayout class has a default gap between components of 0 pixels, but you can specify horizontal and vertical gaps by passing suitable arguments.

Consider the code that creates a grid that can hold up to 12 components in three rows and four columns, with a horizontal gap of 10 pixels and a vertical gap of 14 pixels between components.

p1.setLayout (new GridLayout(3,4,10,14));

The code to implement the described GridLayout is

import java.awt.*;
import java.applet.Applet;

public class DemoGridLayout2 extends Applet {
  public void init () {
    Panel p1 = new Panel ();
    //set layout for panel p1
    p1.setLayout (new GridLayout(3,4,10,14));
    Button b1 = new Button("one");
    Button b2 = new Button("two");
    Button b3 = new Button("three");
    Button b4 = new Button("four");
    Button b5 = new Button("five");
    p1.add(b1);
    p1.add(b2);
    p1.add(b3);
    p1.add(b4);
    p1.add(b5);
  }
}


GridLayout is used to display components added in a tabular manner. Such components will have the same row and column size no matter the size of the container. It is also used for displaying a single component in as much space as possible.

Although GridLayout is useful when you need to lay out a number of components of similar size, you may not have sufficient control over the end result for variable component sizes.
GridBagLayout
The GridBagLayout class is similar to GridLayout in that it uses a grid of rows and columns. However, these rows and columns do not need to be of uniform width and height.

GridBagLayout places the components into the grid's cells but allows the cells to adjust to the required size of the components.

Each component has a GridBagConstraints object that specifies how it should be displayed.

An example of a GridLayout method is

table.setLayout (new GridLayout (5,9));

The flexibility of GridBagLayout lends itself to the layout of many components. Although GridBagLayout is highly functional, it can be very complex to program. You should carefully weigh the benefits of GridBagLayout against the benefits of other more easily programmed layout managers.

For instance, suppose your display incorporates a complicated layout of components and you do not want to use the GridBagLayout class. In this case, you can consider grouping the components into one or more panels and using appropriate layout managers with each panel.
BorderLayout
The BorderLayout manager can arrange components into five separate areas of a container – represented by the BorderLayout constants NORTH, SOUTH, EAST, WEST, and CENTER.

An example of BorderLayout is

import java.awt.*;
import java.applet.Applet;

public class DemoBorderLayout extends Applet {
  public void init () {
    Panel p1 = new Panel();
    //set layout for panel p1
    p1.setLayout (new BorderLayout());
    Button b1 = new Button("Top");
    Button b2 = new Button("Bottom");
    Button b3 = new Button("Right");
    Button b4 = new Button("Left");
    Button b5 = new Button("Center");
    p1 add(b1, BorderLayout.NORTH);
    p1.add(b1, BorderLayout.SOUTH);
    p1.add(b3, BorderLayout.EAST);
    p1.add(b4, BorderLayout.WEST);
    p1.add(b5, BorderLayout.CENTER);
  }
}


You can use the constant values of BorderLayout to specify component positions.

These values can be BorderLayout.NORTH, BorderLayout.SOUTH, BorderLayout.EAST, BorderLayout.WEST, or BorderLayout.CENTER.

Within the confines of the container, the North, South, East, and West components get laid out according to their preferred sizes. The center component then occupies any remaining space.

Here is the code again.

import java.awt.*;
import java.applet.Applet;

public class DemoBorderLayout extends Applet {
  public void init () {
    Panel p1 = new Panel();
    //set layout for panel p1
    p1.setLayout (new BorderLayout());
    Button b1 = new Button("Top");
    Button b2 = new Button("Bottom");
    Button b3 = new Button("Right");
    Button b4 = new Button("Left");
    Button b5 = new Button("Center");
    p1 add(b1, BorderLayout.NORTH);
    p1.add(b1, BorderLayout.SOUTH);
    p1.add(b3, BorderLayout.EAST);
    p1.add(b4, BorderLayout.WEST);
    p1.add(b5, BorderLayout.CENTER);
  }
}


A BorderLayout manager is useful when you want to display standard borders on frames. You can make the center component a panel into which you place all the remaining subcomponents, using another layout manager to manage their layout.

BorderLayout, like GridLayout, can be used when you require the maximum space possible for a component.
CardLayout
The CardLayout class manages components so that only one component is displayed at any given time.

The code for implementing CardLayout in a panel is

Panel card = new Panel ();
card.setLayout(new CardLayout());


You can use methods such as first, last, next, and previous to select the current components to be displayed.

The syntax for the first, last, next, or previous method is

public void first (Container parent)
public void last (Container parent)
public void next (Container parent)
public void previous (Container parent)

The components in a CardLayout layout can, in turn, be containers that hold other components arranged with other layout managers.

This scheme is ideal for creating a tabbed dialog box or a set of properties sheets.

Here is the code again.

Panel card = new Panel ();
card.setLayout(new CardLayout());

0 comments:

Post a Comment