Understanding Layout managers || Using AWT controls, Layout Managers, and Menus || Bcis Notes

Understanding Layout managers || Using AWT controls, Layout Managers, and Menus || Bcis Notes

Understanding Layout managers

Layout management is the process of determining the size and position of components of the managers. By default, each container has a layout manager — an object that performs layout management for the components within the container. Components can provide size and alignment hints to layout managers, but layout managers have the final say on the size and position of those components.
The Java platform supplies five commonly used layout managers: BorderLayout, BoxLayout, FlowLayout, GridBagLayout, and GridLayout. These layout managers are designed for displaying multiple components at once and are shown in the preceding figure. A sixth provided class, CardLayout, is a special-purpose layout manager used in combination with other layout managers. You can find details about each of these six layout managers, including hints for choosing the appropriate one, in Using Layout Managers. 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 setLayout( ) method. If no call to setLayout( ) is made, then the default layout manager is used. Whenever a container is resized (or sized for the first time), the layout manager is used to position each of the components within it.

Flow Layout
The algorithm used by the FlowLayout is to layout Components like words on a page: Left to right, top to bottom.
It fits as many Components into a given row before moving to the next row.
Panel aPanel = new Panel();
aPanel.add(new Button(“Ok”));
aPanel.add(new Button(“Add”));
aPanel.add(new Button(“Delete”));
aPanel.add(new Button(“Cancel”));

Border Layout
The BorderLayout Manager breaks the Container up into 5 regions (North, South, East, West, and Center).
When Components are added, their region is also specified:
Frame aFrame = new Frame();
aFrame.add(“North”, new Button(“Ok”));
aFrame.add(“South”, new Button(“Add”));
aFrame.add(“East”, new Button(“Delete”));
aFrame.add(“West”, new Button(“Cancel”));
aFrame.add(“Center”, new Button(“Recalculate”));

Grid Layout
The GridLayout class divides the region into a grid of equally sized rows and columns.
Components are added left-to-right, top-to-bottom.
The number of rows and columns is specified in the constructor for the LayoutManager.
Panel aPanel = new Panel();
GridLayout theLayout = new GridLayout(2,2);
aPanel.setLayout(theLayout);
aPanel.add(new Button(“Ok”));
aPanel.add(new Button(“Add”));
aPanel.add(new Button(“Delete”));
aPanel.add(new Button(“Cancel”));

CardLayout                                                                                                                                                                              The CardLayout class manages the components in such a manner that only one component is visible at a time. It treats each component as a card that is why it is known as CardLayout. Constructors of CardLayout class:
CardLayout(): creates a card layout with zero horizontal and vertical gaps.
CardLayout(int gap, int gap): creates a card layout with the given horizontal and vertical gap.

Grid Bad Layout

The Java GridBagLayout class is used to align components vertically, horizontally or along their baseline.

The components may not be of the same size. Each GridBagLayout object maintains a dynamic, rectangular grid of cells. Each component occupies one or more cells known as its display area. Each component associates an instance of GridBagConstraints. With the help of constraints object, we arrange the component’s display area on the grid. The GridBagLayout manages each component’s minimum and preferred sizes to determine the component’s size.

You may also like Responding to controls

Be the first to comment

Leave a Reply

Your email address will not be published.


*