Layout Manager:

While adding components to a containers, one important issue is related to the position and size of the component. The components position can be specified in two ways.

  1. Absolute positioning.
  2. Relative positioning.

In absolute positioning, we specify the exact location at which the component should be added. This method gives us fine control and may be the best choice if we design the GUI for just one platform.

For example, a component can be added in middle of container along horizontal direction, by specifying the co-ordinates of top-left corner of the component.

We can calculate these co-ordinates based on the container width and the component width. But the component will not be left in the middle. If the container width changes or the size of the component changes.

The other approach, relative positioning, may not allow us to specify the exact location of the components but may be suitable in the situation where we want that the component should always be in the middle of the container along the horizontal direction.

We can achieve this by specifying the relative position of the component like left, right, center etc. it is very common to center headings in documents using word processors. The headings remain centered even if the width of the page changes this is an example or relative positioning.

In java it is possible to use absolute positioning but it is not advisable. The AWT is designed to use relative positioning. We can specify component layouts with relative specifications, such as the component will be added to the right of the previous component, the component will be added below some other component, the component will appear at top, size of the component will be ¼th of the container width etc. such specifications are useful even without knowledge of component sizes.

Java encourages relative positioning because it is platform independent and the same GUI should work on a wide variety of platforms without modifications.

Built-in java classes called layout managers handle the task of mapping relative positions to actual/physical positions. Java supports many types of layout managers, we will discuss about following layout mangers.

  1. FlowLayout
  2. GridLayout
  3. BorderLayout
  4. CardLayout
  5. GridBagLayout
Two Observations when working with layout managers:
  • We do not have to bear the burden of specifying the exact position and dimension of each component.
  • We no longer had the power to specify the exact position and dimensions of each component.
Why Java uses layout managers?

There are two reasons:

  1. The theory lies in the position that precise layout (that is, specification in pixels of each component’s size and position) is a repetitious and often performed task; therefore, according to OOP’s layout functionality ought to be encapsulated into one or more classes to automate the task.
  2. Certainly the layout managers eliminate a lot of development tedious. Many programmers dislike the idea of layout managers first, but come to appreciate them more and more as tedious choices are eliminated.

  3. The practical reason for having layout manager stems from java’s platform independence. In java, AWT components borrow their behavior from the window system of the underlying hardware on which the JVM is running.
  4. Thus on a Macintosh, an AWT buttons tools like any other Mac button; on a motif platform, a java button looks like any other motif button, and so on. The problem here is that buttons and other components have different sizes when instantiated on different platforms.

    If java encouraged precise pixel-level sizing and positioning, there would be lot of java GUI’s that looked exquisite on their platform of origin and terrible or even unusable, on other platforms.

Copyright 1999- Ducat Creative, All rights reserved.