Layout Managers In Java
In the previous article I discussed about the various Controls in Java like Button, Checkbox, Lists, Scrollbars, Text Fields, and Text Area etc. All of these components have been positioned by the default layout manager. A layout manager automatically arranges the controls within a window by using some type of algorithm. In GUI environments, such as Windows, you can layout your controls by hand. It is possible to lay out Java controls by hand, too, but we do not do so for two main reasons. First, it is very tedious to manually lay out a large number of components. Second, sometimes the width and height information is not yet available when you need to arrange some control, because the native toolkit components have not been realized. This is a chicken-and-egg situation; it is confusing to figure out when it is okay to use the size of a given component to position it relative to another.
Each Container object has a layout manager associated with it. A layout manager is an instance of any class that implements the LayoutManager interface. The layout manager is set by the
Here, layoutObj is a reference to the desired layout manager. If you wish to disable the layout manager and position components manually, pass null for layoutObj. If you do this, you will need to determine the shape and position of each component manually, using the
Each layout manager keeps track of a list of components that are stored by their names. The layout manager is notified each time you add a component to a container. Normally, you will want to use a layout manager.Whenever the container needs to be resized, the layout manager is consulted via its
Java has several predefined LayoutManager classes.
FlowLayout is the default layout manager. FlowLayout implements a simple layout style, which is similar to how words flow in a text editor. Components are laid out from the upper-left corner, left to right and top to bottom. When no more components fit on a line, the next one appears on the next line. A small space is left between each component, above and below, as well as left and right.
The constructors for FlowLayout are shown below:
The first form creates the default layout, which centers components and leaves five pixels of space between each component.
The second form lets you specify how each line is aligned. Valid values for how are as follows:
Here is another type of the CheckboxDemo applet shown in the previous articles, such that it uses left-aligned flow layout.
The BorderLayout class implements a common layout style for top-level windows. It has four narrow, fixed-width components at the edges and one large area in the center. The four sides are referred to as north, south, east, and west. The middle area is called the center.
The constructors defined by BorderLayout are shown below:
The second allows you to specify the horizontal and vertical space left between components in horz and vert, respectively.
BorderLayout defines the following constants that specify the regions:
Here, compObj is the component to be added, and region specifies where the component will be added.
Here is an example of a BorderLayout with a component in each layout area:
Sometimes you will want to leave a small amount of space between the container that holds your components and the window that contains it. To do this, override the
The constructor for Insets is
The values passed in top, left, bottom, and right specify the amount of space between the container and its enclosing window.
When overriding one of these methods, you must return a new Insets object that contains the inset spacing you desire.
Here is the preceding BorderLayout example modified so that it insets its components ten pixels from each border. The background color has been set to cyan to help make the insets more visible.
GridLayout lays out components in a two-dimensional grid. When you instantiate a GridLayout, you define the number of rows and columns.
The constructors supported by GridLayout are shown below:
The second form creates a grid layout with the specified number of rows and columns. The third form allows you to specify the horizontal and vertical space left between components in horz and vert, respectively. Either numRows or numColumns can be zero. Specifying numRows as zero allows for unlimited-length columns. Specifying numColumns as zero allows for unlimited-length rows.
Here is a sample program that creates a 7×3 grid and fills it in with 20 buttons, each labeled with its index:
The CardLayout class is 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 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. We can prepare the other layouts and have them hidden, ready to be activated when needed.
CardLayout provides the following two constructors:
The second form allows you to specify the horizontal and vertical space left between components in horz and vert, respectively.
Use of a card layout requires a bit more work than the other layouts. 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. Next, you add to the appropriate panel the components that form each card. You then add these panels to the panel for which CardLayout is the layout manager. Finally, you add this panel to the main applet panel. Once these steps are complete, you must provide some way for the user to select between cards. One common approach is to include one push button for each card in the deck.
When card panels are added to a panel, they are usually given a name. Most of the time, you will use this form of add( ) when adding cards to a panel:
Here, name is a string that specifies the name of the card whose panel is specified by panelObj.
After you have created a deck, your program activates a card by calling one of the following methods defined by CardLayout:
Calling first( ) causes the first card in the deck to be shown.
To show the last card, call last( ).
To show the next card, call next( ).
To show the previous card, call previous( ).
Both next( ) and previous( ) automatically cycle back to the top or bottom of the deck, respectively.
The show( ) method displays the card whose name is passed in cardName.
The following example creates a two-level card deck that allows the user to select a language. Procedural languages are displayed in one card. Object Oriented languages are displayed in the other card.
On clicking the Procedural languages Button the following output would be displayed:
Re: Layout Managers In Java
If you liked this article do nominate this article for Article of the month - May 2010
|All times are GMT +5.5. The time now is 21:46.|