
In this case, the program must provide a GUI for the user to make the selection. You can also use card layout to let your end user toggle among different displays and choose the one they prefer. With card layout, your program can show the appropriate interface depending on the type of user using the program. For example, the interface shown to an administrator might have additional functionality from the interface shown to a regular user. A program can use this layout to show a different child component to different users. The card layout lets you use one container (usually a panel) to display one out of many possible component children (like flipping cards on a table). P2.add(new Scrollbar(Scrollbar.HORIZONTAL)) P2.add(new Button("An extremely long name"))
#Set the layout manager to use the default flowlayout code
Here is the code for the flow layout panel shown above: The best way to understand flow layout is to resize the demonstration window and notice how the components flow from one row to the other as you make the window wide and narrow.

It places the panel components in rows according to the width of the panel and the number and size of the components.

The flow layout is the default layout manager for all Panel objects and applets. A common mistake people make is to use add(Component), which can result in some of the components not being visible. Note that when using the BorderLayout, you must add components with either the add(String, Component) or the add(Component, Object) method to ensure the components are all added correctly. The following code shows two ways to do it: You can indent the Central area by placing a label with one or more blank characters in the East or West areas. However, if the Central area is left out, the North, South, East, or West areas do not change. If any or all of the North, South, East, or West areas are left out, the Central area spreads into the missing area or areas. Notice how the North and South areas run like a header and footer across the panel. The five areas of a border layout are shown at left. The East (right) and West (left) areas are empty. The demonstration puts the label "Border Layout" in the North area, the OK button in the South area, and the List of fruit trees in the Center area. You do not have to put a component in every area of the border layout. It consists of five fixed areas: North, South, East, West, and Center. The border layout is the default layout manager for all Window objects. The OK button on each right panel layout clears the right panel. If you resize the window, you will see how that layout manager handles the positioning of components during a window resize operation. When you select a layout manager from the left panel, the right panel displays components using the selected layout manager. Here is a screen capture of what you would have seen: You can view AWT and Swing versions of the source code for the demonstrationapplication in the Conclusion section at the end of this article. The demonstration application uses AWT components.

The best way to see how the AWT layout managers behave is to try them out. The Java Development Kit (JDK) 1.2 introduces BoxLayout for placing components from left to right or top to bottom in an AWT or Swing container. This article describes the predefined AWT layout managers in this list. It is easy to use the tLayout method to change the layout manager, and you can define your own layout manager by implementing the interface. Every Abstract Window Toolkit (AWT) and Swing container has a predefined layout manager as its default. The java.awt package provides the following predefined layout managers that implement the interface. As you can see in the figure, the layout manager in effect for the window determines how the two panels are sized and positioned inside the window, and the layout manager in effect for each panel determines how components are sized and positioned inside the panels. For example, a window can contain a panel, which is itself a container. The layout manager in effect for the window determines how the components are sized and positioned inside the window.Ī container can contain another container. For example, a window is a container that contains components such as buttons and labels. What is a Layout Manager?Ī layout manager is an object that controls the size and position (layout) of components inside a Container object. As you read about the different layout managers, you can play with the application to see for yourself how each AWT layout manager arranges components and behaves when the window resizes. This interactive article describes the Abstract Window Toolkit (AWT) predefined layout managers with text and a demonstration application.
