News:
- In an internet driven environment, it is imperative for a company to keep its product or business to the forefront of potential customers' minds. The ...
- A successful enterprise is all about constantly reinventing ways to work more efficiently. In today’s techno age, this translates to testing new too...
- The IT industry plays a pivotal role in providing application development solutions and custom software development to a wide range of industries, i...
- Royal Victorian Eye and Ear Hospital, 2010 Atcomm has been contracted to implement a complex IOP glaucoma management tool which will be distribut...
- We are pleased to announce another implementation of a CMS system for a large Melbourne based fitness center - Star Plate Studio. Atcomm has depl...
Thursday, 20 August 2009 10:26
Layout managers in Java Swing
Layout Managers
Layout managers are used in Java Swing applications to arrange objects when they are added to a Container object. The setLayout() method is used to override default layout managers appropriated to JPanel (FlowLayout) and JFrame (BorderLayout) containers.
This section of the chapter discusses eight important layout managers:
This section of the chapter discusses eight important layout managers:
❑ BorderLayout
❑ BoxLayout
❑ CardLayout
❑ FlowLayout
❑ GridbagLayout
❑ GridLayout
❑ SpringLayout
❑ GroupLayout
❑ BoxLayout
❑ CardLayout
❑ FlowLayout
❑ GridbagLayout
❑ GridLayout
❑ SpringLayout
❑ GroupLayout
BorderLayout
The BorderLayout manager is the default layout for a frame. A BorderLayout uses five regions in its display space. Those regions are generally referred to as NORTH, SOUTH, WEST, EAST, and CENTER. Those regions generally refer to the same attributes that a map would use. The NORTH and SOUTH regions extend to the top and bottom areas of the Container, while the EAST and WEST regions extend from the bottom of the NORTH and top of the SOUTH regions and to the left and right sides of the Container, respectively. The CENTER region occupies all of the residual space that remains in the center of the Container.
The BorderLayout manager is typically generated by instantiating a new BorderLayout class with a constructor that has no parameters or with a constructor that specifies two integer values that specify the horizontal and vertical pixels between components in this fashion: new BorderLayout(int hGap, int vGap).
The following BorderLayout example emulates a test application that quizzes the user with five fairly simple arithmetic queries. As a user sequentially steps through the test questions, a progress bar will track where the test taker is with respect to the end of the test and what the running score is. Figure 4-1 provides a model of the application and shows how the different GUI components will occupy the BorderLayout panel.
The following BorderLayout example emulates a test application that quizzes the user with five fairly simple arithmetic queries. As a user sequentially steps through the test questions, a progress bar will track where the test taker is with respect to the end of the test and what the running score is. Figure 4-1 provides a model of the application and shows how the different GUI components will occupy the BorderLayout panel.

BoxLayout
The BoxLayout manager arranges components horizontally from left to right, or vertically from top to bottom, without the wraparound capability in the FlowLayout manager. Implementation of the BoxLayout manager warrants the instantiation of the BoxLayout class with two parameters, the first being the Container panel that will be displayed, followed by an integer axis value that indicates the placement of the components on the panel. An axis value of Boxlayout.X_AXIS indicates left-to-right layout management, whereas a value of BoxLayout.Y_AXIS signifies a top-to-bottom layout.
The following BoxLayout example will apply the Decorator pattern in its implementation so that users can add behavior dynamically through drag-and-drop operations. The figure below provides a model of the application and how the different image components occupy the BoxLayout panel real estate.

Dynamic behavior transfer occurs when a user drags the Cheese condiment image onto one of the three food item images on the left panel. Some of the benefits of the Decorator pattern are defined in the following table.
FlowLayout
The FlowLayout manager arranges components from left to right in the Container space; if the space on a line is exhausted, the components that are part of this manager will flow to the next line. By default, all components of the FlowLayout manager are centered in a horizontal fashion on each line. Three different constructors can be invoked to instantiate a FlowLayout manager object. The first constructor requires no parameters, and the second constructor requires an integer alignment value that indicates how components will be justified during construction. The last constructor method uses an integer alignment value like the aforementioned method, but also requires two integer values that specify horizontal and vertical gap values for pixel spacing.
Figure 4-5 provides a high-level view of the FlowLayoutPanel application and how the Swing components are positioned on the FlowLayout panel.
Figure 4-5 provides a high-level view of the FlowLayoutPanel application and how the Swing components are positioned on the FlowLayout panel.
Request processing in the FlowLayputPanel application is handled with the Chain of Responsibility pattern that accepts the dollar amount from the user and cascades downward from the four different coin handlers (QuarterHandler, DimeHandler, NickelHandler, PennyHandler) until all the coins have been accounted for in the dollar amount specified by the user.
GridLayout
The GridLayout manager arranges its components in a rectangular, gridlike fashion. When components are added to the GridLayout manager, rows are populated first.
The following GridLayout example processes mouse events on buttons generated with Java 2D classes. Figure 4-7 shows how the buttons are organized on the GridLayoutPanel display.
The following GridLayout example processes mouse events on buttons generated with Java 2D classes. Figure 4-7 shows how the buttons are organized on the GridLayoutPanel display.
GridBagLayout
The GridBagLayout manager manages its components both vertically and horizontally by maintaining a rectangular grid of cells in its display area. Components are manipulated through constraint parameters using the GridBagConstraints class. These constraints specify where a component’s display area should be positioned on the grid and its size using minimum and preferred size attributes.
The table here describes some attributes that can be implemented to position the GridBagLayout manager on a user display.
The table here describes some attributes that can be implemented to position the GridBagLayout manager on a user display.
The following GridBagLayout example applies both the Command and Visitor patterns to handle user events and message generation from Swing component activities. Figure 4-9 provides a model of the application and the component distribution on the GridBagLayout and their listeners.
The GridBagLayoutPanel application will incorporate the Command and Visitor patterns to handle button requests for answers to the questions selected by the user in the different question components. Some of the benefits and shortcomings of these patterns are shown in the following table.
The GridBagLayoutPanel class incorporates the GridBagLayout manager, which allows for the placement of GUI components in a grid formation of rows and columns. The width and height of the rows and columns do not necessarily have to be the same size throughout a panel display, but this sample application maintains consistency across rows and columns for its GUI components:
SpringLayout
The SpringLayout manager lays out its Container components according to user-specified constraint parameters. Each constraint, represented by a Spring object, controls the vertical or horizontal distance between two component edges. The edges can belong to any child of the container or to the container its
The SpringLayout manager does not set the location of its components automatically like some of the other layout managers. Component locations need to be initialized through constraint parameters so that minimum, maximum, and preferred lengths can be contained and bound. The following are some of the fields used to describe the constraints for component placement.
Figure 4-11 demonstrates what the SpringLayout application will look like. Only one tabbed panel will be on display at a time, which will be dictated by the user navigations from the button components at the bottom of the application.
CardLayout
The CardLayout manager organizes its components as a stack of cards, where components are displayed one at a time. This allows components to be easily swapped in and out like a slide show presentation. The following CardLayout example employs the Command and Strategy patterns to encapsulate behavior that will be applied to the user text. Figure 4-13 shows the CardLayout model and the different Swing components applied to that layout manager panel.
The CardLayoutPanel application utilizes the Strategy pattern to apply different algorithms to userspecified text. The Command pattern is used to polymorphically determine what strategy to apply during runtime. Some of the benefits and drawbacks of these two patterns are shown in the following table.
GroupLayout
The GroupLayout manager is a component of the NetBeans framework that is being considered for inclusion in the SE 6 Java framework. The GroupLayout manager is important in that it allows for horizontal and vertical layout positioning in an independent, flexible manner.With this manager, layout groups can be formed in both a sequential and parallel fashion. Sequentially, they are placed one after another, and in parallel, they are placed on top of one another and are aligned with a common reference axis.
Published in
Java