Layouts of AWT In JAVA


In today's article, you learn about Layouts of AWT in Java.

Layouts In AWT

Layouts allow you to format components on the screen in a platform-independent way. Without layouts, you would be forced to place components at explicit locations on the screen, creating obvious problems for programs that need to run on multiple platforms. There's no guarantee that a TextArea or a Scrollbar or any other component will be the same size on each platform, in fact, you can bet they won't be. In an effort to make your Java creations portable across multiple platforms, Sun created a LayoutManager interface that defines methods to reformat the screen based on the current layout and component sizes.
Layout managers try to give programs a consistent and reasonable appearance, regardless of the platform, the screen size, or actions the user might take. Every container has a LayoutManager that is responsible for positioning the component objects within it, regardless of the platform or the screen size. Layout managers eliminate the need to compute component placement on your own, which would be a losing proposition since the size required for any component depends on the platform on which it is displayed.
The standard JDK provides 5 classes that implement the LayoutManager interface. They are FlowLayout, GridLayout, BorderLayout, CardLayout, and GridBagLayout.

1. FlowLayout

The FlowLayout is the default layout for the Panel class, that includes its most famous subclass, Applet. When there are too many components to put, they "wrap" to a new row, similar to a word processor with word wrap enabled. When you add components to the screen, they move left to right (centred within the applet) based upon the order added and the width of the applet. If you resize an applet then the component's move will change based upon the new width and height.  The following shows an example of both before and after resizing.
Here is the code of this program:
  1. import java.awt.*;  
  2. import java.awt.event.*;  
  3. public class FLExample  
  4. {  
  5.   public static void main(String[] args)  
  6.   {  
  7.    Frame frame= new Frame("FlowLayout Frame");  
  8.    Panel pa= new Panel();  
  9.    Button ba1= new Button();  
  10.    Button ba2=new Button();  
  11.    Button ba3=new Button();  
  12.    Button ba4=new Button();  
  13.    Button ba5=new Button();  
  14.    frame.add(pa);  
  15.    pa.setLayout(new FlowLayout());  
  16.    pa.add(new Button("India"));  
  17.    pa.add(new Button("Pakistan"));  
  18.    pa.add(new Button("Japan"));  
  19.    pa.add(new Button("China"));  
  20.    pa.add(new Button("Countries"));  
  21.    frame.setSize(300,300);  
  22.    frame.setVisible(true);  
  23.    frame.addWindowListener(new WindowAdapter()  
  24.     {  
  25.      public void windowClosing(WindowEvent e)  
  26.       {  
  27.        System.exit(0);  
  28.       }  
  29.     });  
  30.   }  
  31. }  

2. GridLayout

You start at row one, column one, then move across the row until it's full, then continue on to the next row. The GridLayout is widely used for arranging components in rows and columns. GridLayout can reposition or resize objects after adding or removing components. As with FlowLayout, the order in which you add components is relevant.  However, unlike FlowLayout, the underlying components are resized to adjust the row-column area, if possible.  Whenever the area is resized, the components within it are resized.  The following shows an example of both before and after resizing.
The same as FlowLayout, some changes exist in Line 3, Line 7 & Line 15.
Line 3: public class GLExample
Line 7: Frame frame= new Frame("GridLayout Frame");
Line 15: pa.setLayout(new GridLayout());

3. BorderLayout

When you add a component to the layout, you must specify which area to place it in. BorderLayout is one of the more unusual layouts provided. It is the default layout for Window, along with its children, Frame and Dialog. BorderLayout provides 5 areas to hold components. These areas are named after the four different borders of the screen, North, South, East, and West, with any remaining space going into the Center area.  The order in which components are added to the screen is not important, although you can have only one component in each area. The following shows a BorderLayout that has one button in each area, before and after resizing.
Here is the code of this program:
The same as FlowLayout, some changes exist in Line 3, Line 7 & Line 15-21.
Line 3-public class BLExample
Line 7- Frame frame= new Frame("BorderLayout Frame");
Line 15-21
pa.setLayout(new BorderLayout());
pa.add(new Button("India"), BorderLayout.NORTH);
pa.add(new Button("Pakistan"), BorderLayout.SOUTH);
pa.add(new Button("Japan"), BorderLayout.EAST);
pa.add(new Button("China"), BorderLayout.WEST);
pa.add(new Button("Countries"), BorderLayout.CENTER);

4. CardLayout

A CardLayout usually manages several components, displaying one of them at a time and hiding the rest. CardLayout lets you assign names to the components it manages and lets you jump to a component by name. With a little work, you can use the CardLayout to create tabbed dialogue boxes or property sheets, that are not currently part of AWT. The CardLayout is a bit on the strange side. All the components are given the same size. Usually, the CardLayout manages a group of Panels (or some other container), and each Panel contains several components of its own. You can also cycle through components in order.
Here is the code of this program:
The same as FlowLayout, some changes exist in Line 3, Line 7 and Line 15:
Line 3-public class CLExample
Line 7: Frame frame= new Frame("CardLayout Frame");
Line 15: pa.setLayout(new CardLayout());

5. GridBagLayout

You provide all the details of each component through instances of the GridBagConstraints class. GridBagLayout is the most sophisticated and complex of the layouts provided in the development kit. With the GridBagLayout, you can organize components in multiple rows and columns, stretch specific rows or columns when space is available, and anchor objects in different corners.  The following shows an example of a GridBagLayout.
The same as FlowLayout, some changes exist in Line3,  Line 7 & Line 15.
Line 3-public class CBLExample
Line 7- Frame frame= new Frame("GridBagLayout Frame");
Line 15- pa.setLayout(new GridBagLayout());