Introduction to Layout Managers in Swing

            Java Swing is used for a variety of web based enterprise applications due to its user friendly layouts, features like copy paste from excel sheets, word, easy calculations on the user web page etc. Swing basically consists of components ranging from the very basic components, like a text field, to some very rich text documents, components with heavy styling properties, tables and so on. This helps us to builds a user interface with a modern and elegant presentation. One of the main concerns of a developer is regarding the layout and placement of components in a way to meet the specified goals. Swing helps them with it by providing several efficient layout managers.

           There are two interfaces included in the AWT package namely LayoutManager or its subclass, LayoutManager2. LayoutManager and all the layout managers should implement either of them. These interfaces provide structured and organized methods to manage layouts, positioning and sizes of the components. So layout managers are classes which implements either LayoutManager or LayoutManager2 and these classes are used to create the custom presentation of the page. Some of them are CardLayout, GridBagLayout, BoxLayout, etc.

           One of the basic layouts is BoxLayout. It lays component either along the horizontal axis or vertical of the mentioned container and manage the positions itself according to the components preferred sizes. An upgraded BoxLayout is a class called Box. Box represents a container with its layout manager as BoxLayout by default. One of the extra features of the Box layout is the provision of inserting invisible spaces, which are blocks without a view but has other properties like position and size. One of those invisible components is filler. Filler can be used in a box layout with three different specifications by calling any of the three methods provided by the class Box. One method returns filler which will fill all the available gaps between the components. The second method returns one with a specified height and the last method returns the invisible component with the specified height and width.

          A FlowLayout is a very simple layout which lays components next to each in a horizontal plane, in a left-right row-row fashion. This layout purely depends on the width of the layout and hence a desirable components position is not attainable.

          Another layout called GridLayout assigns a rectangular grid for each component. For a normal GridLayout, this rectangular grid would have a dimension of one column and one row. We could also specify number of rows and columns for each component and the gap size between rows and columns as well. It forces every component to occupy the grid which could result in undesirable component enlargement or shrinking.

           BorderLayout treats the container as a plane with four borders (north, south, west and east) and a central region. A components position can be specified using the static fields of the class BorderLayout: BorderLayout.EAST, etc.

           One of the most powerful layouts is GroupLayout. The core design principle of this layout manager is dimension independence which means that it treat the components in horizontal and vertical dimensions differently. And while defining layout in each dimensions, the components are further divided into groups which can be sequential or parallel and can contain gaps, components and other groups. Each component should be mandatory defined in each dimension otherwise GroupLayout Exception will be thrown by the compiler.

           There are two types of groups in which these components are packed into: sequential and parallel. In a sequential group, components are placed in a line one after the other either along one axis, just like other layouts in which components are placed along one axis. And in a parallel group, components are placed on top of the other with reference to a common point, which can be again either along the horizontal axis (right-aligned) or along the vertical axis (bottom-aligned). That means a sequential group in one dimension would be a parallel group in the other.