Layout Managers

When you add components to a container, the layout manager determines where your components will be placed relative to other components. If the computer platform varies from user to user, the layout manager takes care of the necessary adjustments. The layout managers in the AWT are:

The flow applet gives a visual example of the different layout managers that can be chosen for a frame. If FlowLayout is chosen but "left", "right", or "center" isn't specified, the default is "center". In order to view different components besides the first one in the CardLayout, code must be written to handle that.

The code for each layout manager used in the frame of the flow applet is explained below.

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

public class flow extends Applet
{
Frame window = new Frame("This is the Frame's Title Bar!");
Button btn = new Button("Destroy the Frame");

Import all the facilities of the AWT and applet that Java has to offer.

Create an applet called flow.

Create an instance of the frame class, initializing the title bar.
Create an instance of the button class with a label.
public void init()
{
add(new Label("The Frame created by this applet has a Border layout."));
add(new Label("Resize the Frame to see how the components"));
add(new Label("automatically adjust. Click on an option below and"));
add(new Label("the layout of the Frame will change."));
add(new Label("You must use the applet's button to delete the Frame."));
CheckboxGroup align = new CheckboxGroup();
add(new Checkbox("Flow Center Arrangement",align,false));
add(new Checkbox("Flow Right Arrangement",align,false));
add(new Checkbox("Flow Left Arrangement",align,false));
add(new Checkbox("Grid (or GridBag) Arrangement",align,false));
add(new Checkbox("Card (Hyper Card) Arrangement",align,false));
add(new Label("Hit this button to"));
add(btn);
add(new Label("."));
window.setLayout(new BorderLayout(10,5));
window.add("North",new Button("One"));
window.add("South",new Button("Two"));
window.add("East",new Button("Three"));
window.add("West",new Button("Four"));
window.add("Center",new Button("Five"));
window.resize(300,200);
window.show();
}
The init method adds labels with directions about the frame created.

A new checkbox group is defined.

Five checkboxes are added to the align group and set at unselected.


A button is added to close the frame.


The initial layout that is set is BorderLayout with a horizontal gap of 10 and a vertical gap of 5.

The five buttons are added to each area of the BorderLayout.

The window is resized and made visible.

public boolean action(Event evt, Object whatAction)
{

if((evt.target instanceof Checkbox))
{
Checkbox currentCheckbox = (Checkbox)evt.target;
boolean checkboxState = currentCheckbox.getState();
if (currentCheckbox.getLabel() == "Flow Center Arrangement")
if (checkboxState)
{
window.hide();
window.dispose();
window.setLayout(new FlowLayout(FlowLayout.CENTER,2,2));
window.resize(300,200);
window.show();
}
if (currentCheckbox.getLabel() == "Flow Right Arrangement")
if (checkboxState)
{
window.hide();
window.dispose();
window.setLayout(new FlowLayout(FlowLayout.RIGHT,2,2));
window.resize(300,200);
window.show();
}
if (currentCheckbox.getLabel() == "Flow Left Arrangement")
if (checkboxState)
{
window.hide();
window.dispose();
window.setLayout(new FlowLayout(FlowLayout.LEFT,2,2));
window.resize(300,200);
window.show();
}
if (currentCheckbox.getLabel() == "Grid (or GridBag) Arrangement")
if (checkboxState)
{
window.hide();
window.dispose();
window.setLayout(new GridLayout(2,3,5,10));
window.resize(300,200);
window.show();
}
if (currentCheckbox.getLabel() == "Card (Hyper Card) Arrangement")
if (checkboxState)
{
window.hide();
window.dispose();
window.setLayout(new CardLayout());
window.resize(300,200);
window.show();
}
return true;
}
if((evt.target instanceof Button))
{
String buttonLabel = (String) whatAction;
if (buttonLabel == "Destroy the Frame")
{
window.hide();
window.dispose();
return true;
}
}
return false;
}
When an action takes place this method tests for it.

If the action was an instance of Checkbox, the event is stored in currentCheckbox. The checkboxState is stored.

If the string on the button is "Flow Center Arrangement", and it is selected, hide the frame named window and dispose of it. Set the layout for flow center with 2 pixels between rows and columns. Resize and show window.


If the string on the button is "Flow Right Arrangement", and it is selected, hide the frame named window and dispose of it. Set the layout for flow right with 2 pixels between rows and columns. Resize and show window.

.

If the string on the button is "Flow Left Arrangement", and it is selected, hide the frame named window and dispose of it. Set the layout for flow left with 2 pixels between rows and columns. Resize and show window.


If the string on the button is "Grid (or GridBag) Arrangement", and it is selected, hide the frame named window and dispose of it. Set the layout for GridLayout with 4 parameters. The number of rows, the number of columns, the horizontal gap, and the vertical gap. If you create a GridLayout with a fixed number of rows, use 0 for the number of columns. If you create a GridLayout with a fixed number of columns, use 0 for the number of rows.
Resize and show window.

If the string on the button is "Card (Hyper Card) Arrangement", and it is selected, hide the frame named window and dispose of it. Set the layout for Card. Resize and show window.


.


If the button is selected and the label is "Destroy the Frame" hide and dispose of the frame named window and return true,

otherwise return false.

public Insets insets()
{
return new Insets(10,20,30,40);
}
}
This method overrides the inset method whose default is 0 to inset from the edges of the container 10 pixels from the top, 20 pixels from the left, 30 pixels from the bottom, and 40 pixels from the right.

 

If you would like to see the code for this Applet click here and, if you like, copy the contents of the screen that loads in your browser , paste it in a text editor and save it in a file called "flow.java".

After compiling this program we need to create an HTML page with a reference to the class file. Save the HTML code in the same directory as the class file. When you load the HTML file in your browser the applet will execute.

Back to Frames