Java Layout Managers

To display a component, it is inevitable to add it to a container. Container is an area on the monitor that can hold and display the components. The frequently used containers are Frame and Panel; the less used and restricted to browser applications is Applet.

To place a component inside a container, there exist two styles – pixel format and position format.

1. Pixel Format

In pixel format we add the component to the container in terms of pixels. The method used is setBounds(). Following program illustrates.

import java.awt.Frame;
import java.awt.Button;

public class PixelFormat extends Frame
      public PixelFormat()
          Button btn = new Button("OK");
          btn.setBounds(75, 100, 110, 50);

          setSize(250, 200);
     public static void main(String args[])
          new PixelFormat();


If the layout is not made null, the default layout manager comes into action. To get affected the pixel format, the above statement is necessary.

btn.setBounds(75, 100, 110, 50);

The above method is defined in java.awt.Component class and can be applied to any component like button and text field etc. The first two parameters 75 and 100 indicate the x and y positions where the component (here, button btn) is to be placed in the container. The last two parameters 110 and 50 give the width and height of the component.

Disadvantages of Pixel Format

The frame we get by default is resizable frame where the user can drag the frame to another size. The programmer has some idea of where to place the component to display an attractive environment. When the user resizes the frame, the idea of the programmer is lost; a button placed at the center initially may look at a corner when the frame is resized by the user.

To overcome the disadvantage of pixel format, the designers introduced position format.

2. Position Format

To give the position, Java comes with a set of classes from java.awt package known as layout managers. Following list gives.

Layout Managers

  1. FlowLayout
  2. BorderLayout
  3. GridLayout
  4. CardLayout
  5. GridBagLayout

Each layout comes with its own style of laying the components in the container. Thorough knowledge of layout managers is very essential for component technology.

Following figure gives the hierarchy of layout managers.

Java Layout Managers

Would like to know Swing Layout Managers also:
BoxLayout Manager
OverlayLayout Manager

Leave a Comment

Your email address will not be published.