Layout policy:

Every java component has preferred size. The preferred size expresses how significant the component would be, barring conflict with a layout manager. Preferred size is generally the smallest size necessary to render the component in a visually meaningful way for example, a button’s preferred size is the size of its label text, plus a little border of space around the text, plus the shadowed decorations that mark the boundary of the button thus a buttons preferred size is “just big enough”. Preferred size is platform dependent since component boundary decorations vary from system to system.

When a layout manager lays out its container’s child components, it has to balance two considerations;

  • layout policy and
  • each component’s preferred size

Priority goes to enforcing layout policy. If honouring a component’s preferred size would mean violating the layout policy, the layout manager overrules its preferred size. Understanding a layout manager means understanding where it will place a component and how it will treat its select size.

  1. FlowLayout:

    The functionality of the flow layout manager is encapsulated in the class java.awt.FlowLayout.

    The Flow Layout Managers arranges components in horizontal rows. It is the default layout manager type for panels and applets.

    The flow layout manager fits as many components as possible into the top row and moves the other component into second row. If no space is left in the second row, the component will move to the third row, and so on. This is quite similar to typing text in a document.

    When we reach at the end of the current line, the next word moves to the next line, and so on. If we change the width or height of the document, the entire document is re-formatted.

    The components always appear, left to right, in the order in which they were added to their container.

    By default, the flow layout manager leaves a gap of five pixels between components in both the horizontal and vertical directions. This default can be changed by calling an overloaded version of the FlowLayout constructor, and passing in the desired horizontal and vertical gaps.

    Within every row, the components are evenly spaced, and the cluster of components is centered. The alignment (sometimes called “justification”) of the clustering can be controlled by passing a parameter to the Flow Layout constructor. The possible values are:

    • FlowLayout.LEFT
    • FlowLayout.CENTER
    • FlowLayout.RIGHT
Example
		import java.awt.*;
		class MyFrame1
		{
		public static void main(String args[ ])
		{
		Frame f=new Frame( );
		FlowLayout flow=new FlowLayout();
		f.setLayout(flow);
		Label l1 = new Label(“Name : “);
		f.add(l1);
		TextField tf1=new TextField(“Matrix”);
		f.add(tf1);
		tf1.setBackground(Color.green);
		tf1.setForeground(Color.red);
		Button b1 = new Button(“ok”);
		Font f1 = new Font(“Arial”, Font.BOLD, 24);
		b1.setFont(f1);
		f.add(b1);
		f.setSize(500,100);
		f.setVisible(true);
		}
		}
	
Note:-

If we reduce the size of the above window and three components do not fit in a single row then components are moved in the next row starting from last component.

Output:
java-layout-policy
Example 2
		import java.awt .*;
		public class MyFrame2 extends Frame
		{
		public static void main (String args [] )
		{
		MyFrame2 f=new MyFrame2();
		f.setLayout(new FlowLayout(FlowLayout.CENTER));
		System.out.println(f.paramString());
		Label l1=new Label(“Name :”);
		f.add(l1);
		TextField tf1=new TextField(“Sample Program”);
		f.add(tf1);
		tf1.setBackground(Color.GREEN);
		tf1.setForeground(Color.RED);
		Button b1=new Button(“OK”);
		b1.setFont(new Font(“Serif”,Font.BOLD,24));
		//b1.setEnabled(false);
		Dimension d = b1.getSize( );
		System.out.println(“Button Size :” + d.width + ” ” + d.height);
		f.add(b1);
		// f.setResizable(false);
		// f.setUndecorated(true);
		f.setVisible(true);
		Dimension d1=b1.getSize( );
		System.out.println(“Button Size: ” + d1.width + ” ” + d1.height);
		Dimension d2=f.getSize();
		System.out.println(“Frame Size: ” + d2.width + ” ” + d2.height);
		try
		{
		Thread.sleep(5000);
		}
		catch(InterruptedException e) { }
		f.setVisible(false);
		f.setSize(500,100);
		f.setVisible(true);
		try
		{
		Thread.sleep(5000);
		}
		catch(InterruptedException e){ }
		f.setVisible(false);
		f.setBounds(20, 20, 100, 100);
		f.setVisible(true);
		Dimension d3=f.getSize();
		System.out.println(“Frame Size : ” + d3.width + ” ” + d3.height);
		System.out.println(f.paramString());
		}
		}
	
Output:
java-layout-policy

frame0,0,0,0×0,invalid,hidden,layout=java.awt.FlowLayout,title=,resizable,normal

Button Size :0 0

Button Size: 52 35

Frame Size: 123 34

Frame Size : 123 100

frame0,20,20,123×100,layout=java.awt.FlowLayout,title=,resizable,normal

GridLayout:

The functionality of the Grid Layout Manager is encapsulated in the class java.awt.GridLayout. The Grid Layout Manager arranges components in tabular format in row and columns.

We can think of container space as a grid of rows and columns. We have to specify the number of rows and columns while specifying the layout.

The Grid Layout Manager fits as many components as the size of the row (number of columns specified in the layout) into the top row and moves the other component into second row, and so on. This is like the spreadsheet, which is divided into a fixed number of rows and columns.

The flow layout manager always honours a component’s preferred size. The GridLayout manager takes the opposite extreme: it ignores a component’s preferred size when it performs a layout in a given space.

Each row and column in a grid layout will be the same size, the overall area available to the layout is divided equally between the number of rows and the numbers of columns. The grid layout uses “row major” notation, i.e. components appear in the order in which they were added, from left to right, row by row.

GridLayout manger behaves strangely when we add few components (that is, significantly fewer than the number of rows times the number of columns) or very many components (that is, more than the number of rows times the number of columns).

If the same components are to be laid in a taller, narrower frame, every component is proportionally taller and narrower and vice-versa.

Example
		import java.awt.* ;
		public class MyFrame6 extends Frame
		{
		public static void main(String args[])
		{
		Frame f = new MyFrame6( );
		f.setLayout(new GridLayout(5,3));
		for(int row=0; row<5; row++)
		{
		f.add(new Label(“Label” + row ));
		f.add(new Button(“Button” + row ));
		f.add(new TextField(“TextField” + row ));
		}
		f.setSize(500,200);
		f.setVisible(true);
		}
		}
	
Output
java-layout-policy
Border Layout:

The functionality of the BorderLayout manager is encapsulated in the class java.awt.BorderLayout. The BorderLayout manager is the default manager for frames, so sooner or later application programmers are sure to come to grips with it.

It enforces a beneficial layout policy, but it is possibly less intuitive than either the flow or grid layout managers. The Flow layout manager always honours a component’s preferred size; the Grid layout manager never does. The Border layout manager does something in between.

The Border Layout Manager divides the available container space into five parts: North, South, East, West and Center. We can add one component to each region.

Each of the five areas may be empty or contain one component (that is, no region is required to contain a component, but the regions can only contain one component).

The Border layout manager honors the North and South components' preferred height and forces them to be exactly as wide as the container. The North and South regions are useful for toolbars, status lines, and any other controls that ought to be as wide as possible, but no higher than necessary.

The East and West regions are the opposite of North and South. In East and West, a component gets its preferred width but has its height contained.

Here a component Extends vertically up to the bottom of the North component (if there is one) or to the top of the container (if there is no North component).

Similarly the component extends vertically down to the South component (if there is one) or to the bottom of the container (if there is no South component).

We can only put a single component in each region well, if that component is a container, then we can get multiple components displayed. The Border layout is not affected by the order in which we add components. Instead, we must specify which of the five regions will receive the component we are adding.

The overloaded version of add( ) takes two parameters:

  • First, the component being added, and
  • Second, an object

Proper use of Border layout manager requires that the second parameter be a constant defined in the Border Layout class itself. The five constants that we should know about are:

BorderLayout.NORTH

BorderLayout.SOUTH

BorderLayout.EAST

Border Layout.WEST

Border Layout.CENTER

The fifth region that a Border layout manager controls is called Center. Center is simply the part of a container that remains after North, South, East, and West have been allocated.

When adding a component to center, it is legal but very unwise, to emit the second parameter to add ( ) call. In Java Platform, the Border layout manager will assume that We mean center; However, in older versions, the behavior was unpredictable, and typically resulted in the component being entirely invisible.

Example
		import java.awt.*;
		public class MyFrame7
		{
		public static void main(String args[])
		{
		Frame f=new Frame( );
		Scrollbar sbRight=new Scrollbar(Scrollbar.VERTICAL);
		f.add(sbRight, BorderLayout.EAST);
		Scrollbar sbLeft=new Scrollbar(Scrollbar.VERTICAL);
		f.add(sbLeft, BorderLayout.WEST);
		Label labelTop=new Label(“This is North”);
		labelTop.setFont(new Font(“Serif”, Font.ITALIC, 36));
		labelTop.setForeground(Color.white);
		labelTop.setBackground(Color.black);
		f.add(labelTop, BorderLayout.NORTH);
		Label labelBottom=new Label(“This is south”);
		labelBottom.setFont(new Font(“Monospaced”, Font.BOLD, 18));
		labelBottom.setForeground(Color.white);
		labelBottom.setBackground(Color.black);
		f.add(labelBottom, BorderLayout.SOUTH);
		f.setSize(500,200);
		f.setVisible(true);
		}
		}
	
Output
java-layout-policy

Copyright 1999- Ducat Creative, All rights reserved.