The setBounds() Method and Its Uses in Java

Mehvish Ashiq Mar 12, 2022
The setBounds() Method and Its Uses in Java

We aim to learn about the setBounds() method and its uses in Java Graphical User Interface (GUI). We’ll briefly learn the setBounds(), why to use it, and how to use it in the code.

the setBounds() Method and Its Uses in Java

In Java Graphical User Interface, the layout managers automatically decide the size and position of the newly added component.

For instance, FlowLayout adds the components in a single row; it only starts a new row if the component does not fit on the current row.

Another example is the BorderLayout that adds the components in the bottom, top, right, left, & center and leaves the extra spaces in the center area.

What shall we do if we are not allowed to use any of these layout managers and are advised to set the size and position of the component manually? Here, the setBounds() method comes into the picture.

We define a component’s bounding rectangle using the setBounds() method.

The setBounds() accepts four arguments. The first two are the x and y coordinate used to position the component.

The second two arguments are width and height used to set the size of the component.

setBounds(int x-coordinate, int y-coordinate, int width, int height)

The frame’s layout manager can be null to manually set the size and position of a component. Let’s understand this by using the code snippet given below.

package com.setbounds.learnsetbounds;

import javax.swing.*;

public class LearnSetBounds{

    public static void main(String[] args) {

 		JFrame jframe = new JFrame("Learning SetBounds Method");
 		//set the size of the window (width and height)
 		jframe.setSize(375, 250);
 		// Setting layout as null
 		jframe.setLayout(null);

 		// Creating Button
 		JButton jbutton = new JButton("Learning setBounds");
 		// Setting position and size of a button
 		jbutton.setBounds(80, 30, 150, 40);

 		//add button to the jframe
 		jframe.add(jbutton);
 		//close window on close event
 		jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 		//center the window on the computer screen
 		jframe.setLocationRelativeTo(null);
 		//show the window
 		jframe.setVisible(true);
    }
}

Output:

setbound method and its uses in java - output one

Update the setBound() method as setBounds(30, 80, 150, 40); and observe the output given below. Remember, x and y specify the top-left location of the component.

Output:

setbound method and its uses in java - output two

In the code snippet given above, we use the javax.swing.JFrame class that works like a primary window where all components (labels, text fields, and buttons) reside. The setSize() method is used to specify the size of the window.

By default, the components are added in the flow on a single row and move to the new row if the component doesn’t fit. The FlowLayout manager causes this default behavior.

As we want to set the size and position of the element manually, we need to use setLayout(null) before using the setBound() method.

Further, the setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) is called when an application got a close window event. The operating system generates the close window event if the user presses the close (X) button on the window further sent to the Java application for processing.

The setVisible(true) method shows the window. Remember, this method can also hide the window if it gets a false value.

Moreover, the setLocationRelativeTo(null) method is used to center the window on the computer screen.

Mehvish Ashiq avatar Mehvish Ashiq avatar

Mehvish Ashiq is a former Java Programmer and a Data Science enthusiast who leverages her expertise to help others to learn and grow by creating interesting, useful, and reader-friendly content in Computer Programming, Data Science, and Technology.

LinkedIn GitHub Facebook