Initially when you add bunch of components to your Java UI you may find scratching your head that where did some of the components go? It even more itches when you don't see any of them when you start. On the other hand after adding lots widgets in your container only to find them crawling around like rascally kittens during resizing. Only because of the annoying matters we turn to layout managers.
The layout of the swing components used is determined by the layout manager used. Java Swing has various kinds of layouts to arrange its components in a container.
There is an interface called LayoutManage interface which is implemented by different kinds of layout.
The size and position of swing component which is inside a container is determined by creating a layout manager.
Some of the implementations of LayoutManager interface are:
1) GridLayout 2) FlowLayout 3) ScrollPaneLayout
Since JDK 1.1,LayoutManager2 interface was introduced.
This LayoutManager2 extends the earlier LayoutManager to provide more functionality.
Implementations of LayoutManager2 interface are:
1) CardLayout 2) GridBagLayout 3) BorderLayout
There are several main tasks of layout manager:
1) Setting the layout manager to a container. 2) Adding a Swing component to a container based on its layout manager. 3) Providing sizing and alignment options. 4) Setting spaces between Swing Components. 5) Setting Components Orientation.
We only care about setting the layouts of content panes and JPanels.
By default content panes has BoderLayout and JPanels have FlowLayout by default unless we set a layout explicitly.
You can define your own layout managers and it should be done for large applications demanding a consistent look. But for purpose of this article we will stick to the standard layout managers.
Shot Description of all Layouts:
Flow Layout:Using this layout components are arranged in a single row that is left to right. Although it starts a new row if its container is not sufficiently wide enough to fit that in. By default JPanel uses this layout.
Grid Layout:It organizes its elements in a rectangular grid having equally sized cells. It creates a rectangular grid with the requested number of rows and columns. These components can be separated with default spacing which can be specified in call to the constructor.
Border Layout:A border layout places its components into five different areas such as: NORTH, SOUTH, EAST, WEST, and CENTER. These are defined by some constants values. Extra spaces are placed in center area. You can optionally use any subset of these regions making it a powerful organizer of relative elements. This is default layout for content panes.
Box Layout:If you want to organize your elements either horizontally (single row) or vertically (single column) this layout is perfect for that. Series of components would be well fitted with this layout. It provides a facility to give maximum sizes of components and also lets you align the same.
Card Layout:It provides a facility of flipping between visible sets of components. These components may be flipped by and event fired. Only one panel is displayed at a time.
Spring Layout:This layout is appropriate for specifying the edges relationships of components.
GridbagLayout:GridBagLayout most flexible and powerful layout manager. It sets the alignment of components by placing them within a grid of cells.It also allows components to span more than one cell by giving gridWidth and gridHight. The rows in the grid can have different heights, and grid columns can have different widths. GridBagConstraint is used for setting the parameters with GridbagLayout.
SPEC INDIA is a leading Java apps development company who understand the requirements of client at every stage of application development process to deliver tailor-made and integrated Java solutions with highest levels of productivity whilst realizing optimum return on client's investment. Hire skilled Java developers to develop plugging business logic into the application, a consistent mechanism for performing tasks such as user data validation, screen navigation, and so forth.