Layout Managers

Dated: 04/23/2001

Layout Managers

Layout Manager automatically arranges your controls within a window by using some type of algorithm.

Each Container object has a layout manager associated with it. A layout manager is an instance of any class that implements the LayoutManager interface.

Is set by

void setLayout (LayoutManager layoutObj)

To disable the layout manager and position components manually, pass null for layoutObj. Then you determine the size and position of each component manually, using setBounds() method defined by Component.

Whenever the container needs to be resized, the layout manager is consulted via minimumLayoutSize() and preferredLayoutSize() methods.

FlowLayout

FlowLayout is the default layout manager.

Components are laid out from the upper-left corner, left to right and top to bottom.

When no mre components fit in a line, the next one appears on the next line.

Constructors

FlowLayout ()

FlowLayout (int how)

FlowLayout (int how, int horz, int vert)

Valid values for how are as follows:

Flowlayout.LEFT

Flowlayout.CENTER

Flowlayout.RIGHT

BorderLayout

BorderLayout class implements a common layout style for top-level windows. Four fixed narrow, fixed-width components at edges and one large area in the center.

Constructors:

BorderLayout ()

BorderLayout (int horz, int vert)

BorderLayout defines the following constants that specify the regions:

BorderLayout.CENTER

BorderLayout.SOUTH

BorderLayout.NORTH

BorderLayout.EAST

BorderLayout.WEST

When adding a component, you will use these constsnts with the following form of add () method.

void add(Component compObj, Object region)

 

Using Insets

Override the getInsets () method that is defined by Container.

Constructor for Insets is:

Insets (int top, int left, int bottom, int right)

Sample Code

import java.awt.*;

import java.applet.*;

/*

<applet code="InsetsDemo" width=300 height=200>

</applet>

*/

public class InsetsDemo extends Applet {

public void init () {

setBackground (Color.cyan);

setLayout (new BorderLayout ());

add(new Button ("This is across the Top"),BorderLayout.NORTH);

add(new Label ("The Footer message goes here"),BorderLayout.SOUTH);

add(new Button ("Right"),BorderLayout.EAST);

add(new Button ("Left"),BorderLayout.WEST);

String msg = "This text appears in the Center of the Border layout";

add(new TextArea (msg), BorderLayout.CENTER);

}

public Insets getInsets () {

return new Insets(10, 10, 10, 10);

}

}

 

 

GridLayout

It lays the components in a 2-dimensional grid.

Constructors:

GridLayout ()

GridLayout (int numRows, int numColumns)

GridLayout (int numRows, int numColumns, int horz, int vert)

Sample Code

import java.awt.*;

import java.applet.*;

/*

<applet code="GridLayoutDemo" width=300 height=200>

</applet>

*/

public class GridLayoutDemo extends Applet {

static final int n = 4;

public void init () {

setLayout (new GridLayout (n,n));

for(int i = 0; i < n; i++){

for (int j = 0; j < n; j++) {

int k = i*n + j;

if (k>0)

add(new Button (" " + k));

}

}

}

}

 

CardLayout

Unique among the other layout managers in that it stores several different layouts.

Each layout can be thought of as being on a separate index card in a deck that can be shuffled so that any card is on the top at a given time. This can be useful for user interfaces with optional components that can be dynamically enabled and disabled upon user input.

Constructors:

CardLayout()

CardLayout(int horz, int vert)

The cards are typically held in an object of type Panel. This Panel must have CardLayout selected as its Layout manager. The cards that form the deck are also typically objects of type Panel. Thus, you must create a Panel that contains the deck and a panel for each card in the deck.

Methods

void add (Component panelObj, Object name)

void first (Container deck)

void last (Container deck)

void next (Container deck)

void previous (Container deck)

void show (Container deck, String cardname) // displays the card whose name is

//passed in cardname

 

Menu Bars and Menus

A top-level can have a menu bar associated with it.

This concept is implemented in java by the following classes:

MenuBar, Menu, and MenuItem

A menu bar contains one or more Menu Objects. Each Menu Object contains a list of MenuItem objects. Each MenuItem object represents something that can be selected by the user. Since Menu is a subclass of MenuItem, a hierarchy of nested submenus can be created.

Can include checkable menu Items using objects of type CheckboxMenuItem

Constructors and Methods:

Menu()

Menu(String optionName)

Menu(String optionName, boolean removable) // if removable true, the pop-up menu

//can be removed and allowed to float free

MenuItem ()

MenuItem (String itemName)

MenuItem (String itemName, MenuShortcut keyAccel) //keyAccel is the menu

//shortcut for this item

void setEnabled (boolean enabledFlag) //to enable menu item

boolean isEnabled ()

void setLabel (String newName)

String getLabel ()

CheckboxMenuItem ()

CheckboxMenuItem (String itemName)

CheckboxMenuItem (String itemName, boolean on)

boolean getState ()

void setState (boolean checked)

MenuItem add (MenuItem item)

Menu add (Menu menu)

Object getItem()

getItem () method of ItemEvent returns a reference to the item that generated this event.

Dialog Boxes

Dialog boxes are primarily used to obtain user input.

They are always child windows to a top-level window.

Don’t have menu bars.

Constructors:

Dialog (Frame parentWindow, boolean mode)

Dialog (Frame parentWindow,String title, boolean mode)

When a model dialog box is active, all input is directed to it until it is closed. This means you can not acces other parts of your program until you have closed the dialog box. When modeless dialog box is active, input focus can be directed to another window in your program.

When the dialog box is closed, dispose () is called. This method is defined by Window, and it frees all system resources associated with the dialog box window.

FileDialog

Java provides a built-in dialog box that lets the user specify a file.

Constructors:

FileDialog (Frame parent)

FileDialog (Frame parent, String boxName)

FileDialog (Frame parent, String boxName, int how)

how is FileDialog.LOAD (the box is selecting a file to read) or FileDialog.SAVE (the box is selecting a file for writing).

String getDirectory ()

String getFile ()