Java Programming

Layout Manager in Java: FlowLayout, BorderLayout, GridLayout with examples

What is Layout Manager in java?

We have already seen that a layout manager can arrange the various Components in a container and such a layout Manager through an object of a class that implements the Layout Manager interface, is produced. This Layout Manager interface therefore defines methods which are necessary for the arrangement of AWT and Swing components. Java provides numerous classes that implement this interface and ultimately differ in that they divide the container area into different Divide areas. The layout managers distribute the entire space the container area depends on the components entered, whereby (each according to layout) is partially inserted space or components in their Size adjusted or not displayed at all.

Amazon Purchase Links:

Top Gaming Computers

Best Laptops

Best Graphic Cards

Portable Hard Drives

Best Keyboards

Best High Quality PC Mic

Computer Accessories

*Please Note: These are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!

What are the different types of layout manager in java?

The three most frequently used layout managers are FlowLayout, BorderLayout and GridLayout; with them, we will therefore be in the deal with the following sections. There are also some specialized ones Layout variants such as BoxLayout, CardLayout, GridBagLayout or overlay layout. The standard layout in the container classes is BorderLayout discontinued. The only exception is the JPanel class (a Component that we will get to know in my next article).

FlowLayout as Layout Manager:

To arrange the components in a container in a fluid manner, one uses an object of the class FlowLayout as a layout manager. “Flowing” means here that the components are inserted into the container line by line from left to right Be observed. That is, the components are so long in the order their insertion from left to right side by side until there is no more space for the next component is available and started with a new line which is then filled in the same way. The orientation of the components the line is centered by default. Between the components there is a distance of 5 pixels horizontally and vertically. The size of the components is not changed. In the class FlowLayout we find the following constructors:

public FlowLayout ()

creates a FlowLayout object with the default settings (centered Alignment of the lines, 5-pixel spacing).

public FlowLayout (int align)

creates a FlowLayout object with an alignment according to align and the Standard setting for the distances.

public FlowLayout (int align, int h, int v)

creates a FlowLayout object with an alignment according to align and horizontal and vertical distances of h and v pixels.

The predefined constants LEFT (for left-justified), RIGHT (for right-justified) and CENTER (for centered alignment) as final class variables of the class FlowLayout available.

In our example program

We again work with our self-written class and a JLabel called label  with four components of that type. In the constructor we create our Testing class for these four label components the corresponding objects of the label_class class, where we set the foreground color white and background color black. We also use italic serif in every label 28 point font.

Our program

If you start the Testing class, then initially (accordingly the space required by the relatively large labeled labels) only ever two of the Labels in one line.

Layout Manager

you change with the Mouse the width of our frame, so there is only space for one label per line

Layout Manager

Or even for three labels per line

Layout Manager

BorderLayout as Layout Manager:

To divide the container area into the five areas “North”, “South”, “West”, “East” and “Center” are used Object of the class BoderLayout as layout manager. In each of these five areas a component can be inserted, making a total of five components may appear. While the size of the components in the north and South by their usual height and that of the components in the west and east is determined by its usual width, the size of the central area may vary according to the size of the container. Accordingly, the size of the there added component adapted. The following constructors of the BorderLayout class are available:

public BorderLayout()

creates a BorderLayout object with the default setting (0 spaces between the areas).

BorderLayout (int h, int v)

creates a BorderLayout object with horizontal or vertical Distances of h and v pixels between the areas.

When adding components to a container using the add method the second parameter can also be used to determine the area into which the Component is to be inserted. This is done using one of the Class constants NORTH, SOUTH, WEST, EAST predefined in BorderLayout and CENTER. Calling up the add method without “direction” corresponds a call with BorderLayout.CENTER.

To clarify the different areas of the border layout, we have the labels placed there (again objects of our self-written class code: code:

Layout Manager

colored in different shades of gray. If you change the width with the mouse of our frame, one can determine that the size of the edge components remains the same, while the component in the center is dynamically related to the total size Adjust the size of the frame.

Layout Manager

GridLayout as Layout Manager:

If you want the container area in grid or table-like arranged cells split, you use an object of the class GridLayout as a layout Manager. You already specify how many lines are called when the constructor is called or columns should be created. All inserted components are then displayed in the same size according to this specification, so that the Cell’s available space is completely filled. The default is between the components no distance. The GridLayout class provides the following constructors:

public GridLayout ()

creates a GridLayout object with standard setting (no spaces between cells).

public GridLayout (int z, int s)

creates a GridLayout object with z rows and s columns and the default setting for the distances. Either z or s can also have the value 0 have what stands for “any number”.

public GridLayout (int z, int s, int h, int v)

creates a GridLayout object with z rows and s columns and horizontal ones and vertical distances of h and v pixels. Either z or s can be used also have the value 0, which stands for “any number”.

In this layout, the components are in the order of the add calls inserted in the table or the grid. It starts in the top line, these are filled from left to right and each with the one below Line continued.

Here I am using again my self-written class for class the object code: code:

Layout Manager

We have one to clarify the definition of the respective cell size Grid layout with two rows and three columns as well as a horizontal resp. vertical cell spacing of 10 or 40 pixels selected. The different labels in the individual cells we have again colored differently and with different sized fonts. In the above figure is now in particular to recognize that the cell size is not sufficient for all labels is so that the label text, if necessary, is automatically abbreviated.

Engr Fahad

My name is Shahzada Fahad and I am an Electrical Engineer. I have been doing Job in UAE as a site engineer in an Electrical Construction Company. Currently, I am running my own YouTube channel "Electronic Clinic", and managing this Website. My Hobbies are * Watching Movies * Music * Martial Arts * Photography * Travelling * Make Sketches and so on...

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button