The FlowLayout Manager The BorderLayout Manager

J.E.D.I.

7.4 Layout Managers

The position and size of the components within each container is determined by the layout manager. The layout managers governs the layout of the components in the container. These are some of the layout managers included in Java. 1. FlowLayout 2. BorderLayout 3. GridLayout 4. GridBagLayout 5. CardLayout 6. The layout manager can be set using the setLayout method of the Container class. The method has the following signature. void setLayoutLayoutManager mgr If you prefer not to use any layout manager at all, you pass null as an argument to this method. But then, you would have to position the elements manually with the use of the setBounds method of the Component class. public void setBoundsint x, int y, int width, int height The method controls the position based on the arguments x and y, and the size based on the specified width and height. This would be quite difficult and tedious to program if you have several Component objects within the Container object. Youll have to call this method for each component. In this section, youll learn about the first three layout managers.

7.4.1 The FlowLayout Manager

The FlowLayout is the default manager for the Panel class and its subclasses, including the Applet class. It positions the components in a left to right and top to bottom manner, starting at the upper-lefthand corner. Imagine how you type using a particular word editor. This is how the FlowLayout manager works. It has three constructors which are as listed below. FlowLayout Constructors FlowLayout Creates a new FlowLayout object with the center alignment and 5-unit horizontal and vertical gap applied to the components by default. FlowLayoutint align Creates a new FlowLayout object with the specified alignment and the default 5-unit horizontal and vertical gap applied to the components. FlowLayoutint align, int hgap, int vgap Creates a new FlowLayout object with the first argument as the alignment applied and the hgap-unit horizontal and vgap-unit vertical gap applied to the components. Table 19: FlowLayout constructors The gap refers to the spacing between the components and is measured in pixels. The Introduction to Programming II Page 95 J.E.D.I. alignment argument should be one of the following: 1. FlowLayout.LEFT 2. FlowLayout.CENTER 3. FlowLayout.RIGHT What is the expected output of the following program? import java.awt.; class FlowLayoutDemo extends Frame { public static void mainString args[] { FlowLayoutDemo fld = new FlowLayoutDemo; fld.setLayoutnew FlowLayoutFlowLayout.RIGHT, 10, 10; fld.addnew ButtonONE; fld.addnew ButtonTWO; fld.addnew ButtonTHREE; fld.setSize100, 100; fld.setVisibletrue; } } Shown below is a sample output running on Windows platform. Figure 7.4: Running FlowLayoutDemo Introduction to Programming II Page 96 J.E.D.I.

7.4.2 The BorderLayout Manager

The BorderLayout divides the Container into five parts- north, south, east, west and the center. Each components is added to a specific region. The north and south regions stretch horizontally whereas the east and west regions adjust vertically. The center region, on the other hand, adjusts in both horizontally and vertically. This layout is the default for Window objects, including objects of Windows subclasses Frame and Dialog type. BorderLayout Constructors BorderLayout Creates a new BorderLayout object with no spacing applied among the different components. BorderLayoutint hgap, int vgap Creates a new BorderLayout object with hgap-unit horizontal and vgap-unit spacing applied among the different components. Table 20: BorderLayout constructors Like in the FlowLayout manager, the parameters hgap and vgap here also refers to the spacing between the components within the container. To add a component to a specified region, use the add method and pass two arguments: the component to add and the region where the component is to be positioned. Note that only one component can be placed in one region. Adding more than one component to a particular container results in displaying only the last component added. The following list gives the valid regions are predefined fields in the BorderLayout class. 1. BorderLayout.NORTH 2. BorderLayout.SOUTH 3. BorderLayout.EAST 4. BorderLayout.WEST 5. BorderLayout.CENTER Here is a sample program demonstrating how the BorderLayout works. import java.awt.; class BorderLayoutDemo extends Frame { public static void mainString args[] { BorderLayoutDemo bld = new BorderLayoutDemo; bld.setLayoutnew BorderLayout10, 10; may remove bld.addnew ButtonNORTH, BorderLayout.NORTH; bld.addnew ButtonSOUTH, BorderLayout.SOUTH; bld.addnew ButtonEAST, BorderLayout.EAST; bld.addnew ButtonWEST, BorderLayout.WEST; bld.addnew ButtonCENTER, BorderLayout.CENTER; bld.setSize200, 200; bld.setVisibletrue; } } Here is a sample output of this program. The second figure shows the effect of resizing Introduction to Programming II Page 97 J.E.D.I. the frame. Introduction to Programming II Page 98 J.E.D.I. Figure 7.5: Running BorderLayoutDemo

7.4.3 The GridLayout Manager