Friday, October 7, 2011

Java Layout Manager [to be continued]

Time to discuss several kinds of layout managers in java used in java. The layout managers are needed to arrange the components. here the kinds of layout managers to be discussed. The best way to learn is getting the code, run and trace.
Note: sorry if the samples do not look good
  • The Border Layout
  • The Box Layout
  • The Flow Layout
  • The Grid Layout
  • The Card Layout
Here is the source code hopefully the link works if it does the download the file and open the .bat file to see the program


The Border Layout
Sample of components arranged in the Border layout

The code to generate that is in the Launcher class specifically the BorderLayout(String[] s) method. This layout divides the container using this layout manager into five sections: north,south, east, west, and center.
To insert a component we must use an add method specifically add(Component, some constant)
The constants are:

  • BorderLayout.NORTH
  • BorderLayout.SOUTH
  • BorderLayout.WEST
  • BorderLayout.EAST
  • BorderLayout.CENTER
So the constant used when adding a components will determine the part where the component will be inserted in the layout. For example  JF.add(new JLabel("This is the north",JLabel.CENTER),BorderLayout.NORTH); that line of code will insert a JLabel to the northern part of the layer of JF.

The Box Layout

This layout stacks components from top to bottom or left to right. The constructor has 2 parameters. First the container of the of the box layout and a constant that sets up vertical or horizontal alignment. For example

JPanel optionPane = new JPanel();
BoxLayout box = new BoxLayout(optionPane,
BoxLayout.Y_AXIS);
 The lines of code above gives instruction to make the JPanel utilize the Box layout. For horizontal stacking, the BoxLayout.X_AXIS constant can be the second argument in the parameters.
Please note that  in horizontal alignment, the box layout manager attempts to give each component the same height. In vertical alignment, it attempts to give each one the same width.

Flow layout

The flow layout is the simplest layout manager to use since components are added like how characters are written in a word processor application.
It can have the following constructors

  • new FlowLayout();
  • new FlowLayout(CONSTANT);
The FlowLayout(int, int, int) constructor takes the following three arguments, in order:

  • The alignment, which must be one of five class variables of FlowLayout: CENTER,LEFT, RIGHT, LEADING, or TRAILING
  • The horizontal gap between components, in pixels
  •  The vertical gap, in pixels

sample: FlowLayout flo = new FlowLayout(FlowLayout.CENTER, 15, 5);


The default alignment  of added components are centered, but the constants  FlowLayout.LEFT or FlowLayout.RIGHT to change the alignment of components to be added.

The Grid Layout

Components are arranged in rows and columns. Components are added first to the top row of the grid, beginning with the leftmost grid cell and continuing to the right. When all the cells in the top row are full, the next component is added to the leftmost cell in the second row of the grid—if there is a second row and so on.

Constructors:

  • GridLayout()- default of one column per component, in a single row. May form something like the horizontal box layout
  • GridLayout(int rows, int cols)- specified number of rows and columns.
  • GridLayout(int rows, int cols, int hgap, int vgap) specified number of rows and columns. and gaps
The default gap between components under a grid layout is 0 pixel in both vertical and horizontal directions much like the Flowlayout where the vertical and horizontal gaps can also be modified. The components under this kind of layout expand to fill the space available to them in each cell unlike other layouts.

Card Layout
With the card layout the componets shown  can change

A card layout is a group of containers or components displayed one at a time. 

The recommended declaration is similar to this. CardLayout cc = new CardLayout();
then the container (Like a JFrame) that will use that layout will have to use the method setLayout(cc);
A different add() method has to be used. Here it is add(String,Component). The container must support that add() method to use the Card Layout where the String is the title/name of the "Card" which is the name that signals to what component to be displayed which is the component beside the string parameter.

After adding the programmer can instruct on what to show when the program is running but a show method must be used. For Example:

cc.show(this, “Fact Card”); or cc.show(JF.getContentPane(), “Fact Card”);

The trigger in changing what to show can happen from an event or other methods.



Reference:


Cadenhead R. and Lemay L. (2007). Sams teach yourself Java 6 in 21 days. United States of  America: Sams Publishing.