Introduction to Layout Management Core Java

Before we go on to discussing individual Swing components, such as text fields and radio buttons, we briefly cover how to arrange these components inside a frame. Unlike Visual Basic, the JDK has no form designer. You need to write code to position (lay out) the user interface components where you want them to be.

Of course, if you have a Java-enabled development environment, it will probably have a layout tool that automates some or all of these tasks. Nevertheless, it is important to know exactly what goes on “under the hood” because even the best of these tools will usually require hand-tweaking.

Let’s start by reviewing the program that used buttons to change the background color of a frame.

A panel with three buttons

A panel with three buttons

The buttons are contained in a JPanel object and are managed by the flow layout manager, the default layout manager for a panel. Figure below shows what happens when you add more buttons to the panel. As you can see, a new row is started when there is no more room.

panel with six buttons managed by a flow layout

panel with six buttons managed by a flow layout

Moreover, the buttons stay centered in the panel, even when the user resizes the frame.

Changing the panel size rearranges the buttons automatically

Changing the panel size rearranges the buttons automatically

In general, components are placed inside containers, and a layout manager determines thepositions and sizes of the components in the container.Buttons, text fields, and other user interface elements extend the class Component. Componentscan be placed inside containers such as panels. Because containers can themselvesbe put inside other containers, the class Container extends Component. the inheritance hierarchy for Component.

Inheritance hierarchy for the Component class

Inheritance hierarchy for the Component class

Each container has a default layout manager, but you can always set your own. For example, the statement

uses the GridLayout class to lay out the components in the panel. You add components to the container. The add method of the container passes the component and any placement directions to the layout manager.

Border Layout

The border layout manager is the default layout manager of the content pane of every JFrame. Unlike the flow layout manager, which completely controls the position of each component, the border layout manager lets you choose where you want to place each component. You can choose to place the component in the center, north, south, east, or west of the content pane.

Border layout

Border layout

For example:

The edge components are laid out first, and the remaining available space is occupied by the center. When the container is resized, the dimensions of the edge components are unchanged, but the center component changes its size. You add components by specifying a constant CENTER, NORTH, SOUTH, EAST, or WEST of the BorderLayout class. Not all of the positions need to be occupied. If you don’t supply any value, CENTER is assumed.

Unlike the flow layout, the border layout grows all components to fill the available space. (The flow layout leaves each component at its preferred size.) This is a problem when you add a button:

what happens when you use the preceding code fragment. The button has grown to fill the entire southern region of the frame. And, if you were to add another button to the southern region, it would just displace the first button.

A single button managed by a border layout

A single button managed by a border layout

You solve this problem by using additional panels. For example. The three buttons at the bottom of the screen are all contained in a panel. The panel is put into the southern region of the content pane.

Panel placed at the southern region of the frame

Panel placed at the southern region of the frame

To achieve this configuration, first create a new JPanel object, then add the individual buttons to the panel. The default layout manager for a panel is a FlowLayout, which is a good choice for this situation. You add the individual buttons to the panel, using the add method you have seen before.The position and size of the buttons is under the control of the FlowLayout manager. This means the buttons stay centered within the panel, and they do not expand to fill the entire panel area. Finally, you add the panel to the content pane of the frame.

The border layout expands the size of the panel to fill the entire southern region.

Grid Layout

The grid layout arranges all components in rows and columns like a spreadsheet. All components are given the same size. The calculator program in Figure below uses a grid layout to arrange the calculator buttons. When you resize the window, the buttons grow and shrink, but all buttons have identical sizes.

A calculator

A calculator

In the constructor of the grid layout object, you specify how many rows and columns you need.

You add the components, starting with the first entry in the first row, then the second entry in the first row, and so on.

Listing below is the source listing for the calculator program. This is a regular calculator, not the “reverse Polish” variety that is so oddly popular in Java tutorials. In this program, we call the pack method after adding the component to the frame. This method uses the preferred sizes of all components to compute the width and height of the frame. Of course, few applications have as rigid a layout as the face of a calculator. In practice, small grids (usually with just one row or one column) can be useful to organize partial areas of a window. For example, if you want to have a row of buttons with identical size, then you can put the buttons inside a panel that is governed by a grid layout with a single row.

Calculator.java



Face Book Twitter Google Plus Instagram Youtube Linkedin Myspace Pinterest Soundcloud Wikipedia

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Core Java Topics