Layout Managers in Java


Java Layout Managers

The GUI is made up of a number of components; the Layout Managers affect the content pane.

Layout Managers are provided to arrange GUI components on a container for presentation purposes. This allows the programmer to concentrate on the basic "look and feel" and lets the layout managers process most of the layout details.

There are many layout managers.

Basic Layout Managers
FlowLayout
Default for java.applet.Appletjava.awt.Panel and java.swing.JPanel. Places components sequentially (left to right) in the order they were added. You can specify the order.
BorderLayout
Default for the content panes of JFrame and JApplets. Arranges the components into five areas: North, South East, West and Center
GridLayout
Arranges the components into rows and columns
Advanced Layout Managers
BoxLayout
Allows components to be arranged left-to-right or top-to-bottom in a container
CardLayout
Stacks components like a deck of cards
GridBagLayout
similar to GridLayout. unlike GridLayout each component size can vary and components can be added in any order.

Java BorderLayout

The BorderLayout is used to arrange the components in five regions: north, south, east, west and center. Each region (area) may contain one component only. It is the default layout of frame or window. The BorderLayout provides five constants for each region:
  1. public static final int NORTH
  2. public static final int SOUTH
  3. public static final int EAST
  4. public static final int WEST
  5. public static final int CENTER

Constructors of BorderLayout class:

  • BorderLayout(): creates a border layout but with no gaps between the components.
  • JBorderLayout(int hgap, int vgap): creates a border layout with the given horizontal and vertical gaps between the components.
Java GridLayout

The GridLayout is used to arrange the components in rectangular grid. One component is displayed in each rectangle.

Constructors of GridLayout class
  1. GridLayout(): creates a grid layout with one column per component in a row.
  2. GridLayout(int rows, int columns): creates a grid layout with the given rows and columns but no gaps between the components.
  3. GridLayout(int rows, int columns, int hgap, int vgap): creates a grid layout with the given rows and columns alongwith given horizontal and vertical gaps.

Java FlowLayout

The FlowLayout is used to arrange the components in a line, one after another (in a flow). It is the default layout of applet or panel.

Fields of FlowLayout class
  1. public static final int LEFT
  2. public static final int RIGHT
  3. public static final int CENTER
  4. public static final int LEADING
  5. public static final int TRAILING
Constructors of FlowLayout class
  1. FlowLayout(): creates a flow layout with centered alignment and a default 5 unit horizontal and vertical gap.
  2. FlowLayout(int align): creates a flow layout with the given alignment and a default 5 unit horizontal and vertical gap.
  3. FlowLayout(int align, int hgap, int vgap): creates a flow layout with the given alignment and the given horizontal and vertical gap.

Java BoxLayout

The BoxLayout is used to arrange the components either vertically or horizontally. For this purpose, BoxLayout provides four constants. They are as follows:

Fields of BoxLayout class

  1. public static final int X_AXIS
  2. public static final int Y_AXIS
  3. public static final int LINE_AXIS
  4. public static final int PAGE_AXIS

Constructor of BoxLayout class

BoxLayout(Container c, int axis): creates a box layout that arranges the components with the given axis.

Java 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
  1. CardLayout(): creates a card layout with zero horizontal and vertical gap.
  2. CardLayout(int hgap, int vgap): creates a card layout with the given horizontal and vertical gap.
Commonly used methods of CardLayout class
  • public void next(Container parent): is used to flip to the next card of the given container.
  • public void previous(Container parent): is used to flip to the previous card of the given container.
  • public void first(Container parent): is used to flip to the first card of the given container.
  • public void last(Container parent): is used to flip to the last card of the given container.
  • public void show(Container parent, String name): is used to flip to the specified card with the given name.

Comments

Popular posts from this blog

પટેલ સમાજનો ઈતિહાસ જાણો : કોણ અને ક્યાંથી આવ્યા હતા પાટીદારો

Python HTML Generator using Yattag Part 1

Java Event Delegation Model, Listener and Adapter Classes