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

package testing;
/**
 *
 * @author Fawad khan
 */
import java.awt. *;
import javax.swing. *;

public class Testing extends JFrame {
Container c; // container of this frame 
label_class label[]=new label_class[4];
public Testing()
{

c=getContentPane();
c.setLayout(new FlowLayout());
for(int i =0; i< 4; i++){

   
    label[i]=new label_class("number "+(i+1),new Color(255,255,255,255),new Color(0,0,0,255));
    label[i].setFont(new Font("serif",Font.ITALIC,28));
    
    
}
for(int i=0; i<4; i++)
{
c.add(label[i]);
}
}
 public static void main (String [] args) {
 Testing window = new Testing();
 window.setTitle ("Java flowLayout");
 window.setSize (700,300);
 window.setVisible (true);
 window.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
}
}

We again work with our self-written class label_class.java 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 label_class.java program

package testing;
import java.awt.*;
 import javax.swing.*;
 public class label_class extends JLabel {
public label_class(String text,Color fG,Color bG) { // constructr
// Transfer of the label text to the super constructor
super(text);
// Set the background of the label to opaque
setOpaque(true);
// Set the color of the text on the label
 setForeground(fG);
// Set the background color of the label
 setBackground(bG);
 }
}

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 label_class.java).

Label_class.java code:

package testing;
import java.awt.*;
 import javax.swing.*;
 public class label_class extends JLabel {
public label_class(String text,Color fG,Color bG) { // constructr
// Transfer of the label text to the super constructor
super(text);
// Set the background of the label to opaque
setOpaque(true);
// Set the color of the text on the label
 setForeground(fG);
// Set the background color of the label
 setBackground(bG);
 }
}

Testing.java code:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package testing;
/**
 *
 * @author Fawad khan
 */
import java.awt. *;
import javax.swing. *;

public class Testing extends JFrame {
Container c; // container of this frame 
label_class label[]=new label_class[5];
public Testing()
{

c=getContentPane();
c.setLayout(new BorderLayout());
label[0]=new label_class("North",Color.BLACK, Color.WHITE);
label[1]=new label_class("South",Color.WHITE, Color.LIGHT_GRAY);
label[2]=new label_class("East",Color.WHITE, Color.GRAY);
label[3]=new label_class("West",Color.WHITE, Color.DARK_GRAY);
label[4]=new label_class("Center",Color.WHITE, Color.BLACK);

for(int i=0;i<5; i++)
{
label[i].setFont(new Font("SansSerif", Font.BOLD,14));
label[i].setHorizontalAlignment(JLabel.CENTER);
}
c.add(label[0],BorderLayout.NORTH);
c.add(label[1],BorderLayout.SOUTH);
c.add(label[2],BorderLayout.EAST);
c.add(label[3],BorderLayout.WEST);
c.add(label[4],BorderLayout.CENTER);


}
 public static void main (String [] args) {
 Testing window = new Testing();
 window.setTitle ("Java BorderLayout");
 window.setSize (700,300);
 window.setVisible (true);
 window.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
}
}

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 label_class.java for class the object

label_class.java code:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package testing;

import java.awt.*;
 import javax.swing.*;
 public class label_class extends JLabel {
public label_class(String text,Color fG,Color bG) { // constructr
// Transfer of the label text to the super constructor
super(text);
// Set the background of the label to opaque
setOpaque(true);
// Set the color of the text on the label
 setForeground(fG);
// Set the background color of the label
 setBackground(bG);
 }
}

Testing.java code:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package testing;
/**
 *
 * @author Fawad khan
 */
import java.awt. *;
import javax.swing. *;

public class Testing extends JFrame {
Container c; // container of this frame 
label_class label[]=new label_class[6];
public Testing()
{

c=getContentPane();
c.setLayout(new GridLayout(2,3,10,40));
for (int i = 0; i < 6; i++) {
int rgbFg = 255 - i*50;
int rgbBg = i*50;
label[i] = new label_class("Number " + (i+1),
new Color(rgbFg,rgbFg,rgbFg),
 new Color(rgbBg,rgbBg,rgbBg));
 label[i].setFont(new Font("Serif",Font.ITALIC,10 + i*3));
 }

for(int i=0;i<5; i++)
{
label[i].setFont(new Font("SansSerif", Font.BOLD,14));
label[i].setHorizontalAlignment(JLabel.CENTER);
}
for (int i = 0; i < 6; i++) {
 c.add(label[i]);
 }


}
 public static void main (String [] args) {
 Testing window = new Testing();
 window.setTitle ("Java GridLayout");
 window.setSize (700,300);
 window.setVisible (true);
 window.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
}
}

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...

Leave a Reply

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

Back to top button