Place Component in Bottom Right Corner with BorderLayout in Java



In this article, we will learn how to place a component, specifically a button, in the bottom-right corner of a Java Swing application using the BorderLayout manager. The BorderLayout is one of the most commonly used layout managers in Java, and it allows you to place components in five different regions: North, South, East, West, and Center. We will demonstrate how to use BorderLayout to ensure the component is placed precisely in the bottom-right corner of the window.

Steps to place the component in the bottom-right corner

Following are the steps to place the component in the bottom-right corner with BorderLayout ?

  • We will start by importing the necessary classes from the javax.swing and java.awt packages including JButton, JPanel, JFrame, and BorderLayout.
  • After that, we will create a JButton component and set its background and text color.
  • Create a JPanel and use the BorderLayout manager to arrange the button in the bottom-right corner by adding the button to the LINE_END (right side) of the panel.
  • Create another JPanel, also using BorderLayout, and place the previous panel (containing the button) at the PAGE_END (bottom) position of this panel.
  • Add this main panel to the frame, set the size of the frame, and display the window.

Java program to place components in the bottom-right corner

The following is an example of placing a component in the bottom-right corner with BorderLayout ?

package my;
import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class SwingDemo {
   public static void main(String[] args) {
      JButton button = new JButton("This is Demo Text!");
      button.setBackground(Color.blue);
      button.setForeground(Color.white);
      JPanel bottomPanel = new JPanel(new BorderLayout());
      bottomPanel.add(button, BorderLayout.LINE_END);
      JPanel mainPanel = new JPanel(new BorderLayout());
      mainPanel.add(bottomPanel, BorderLayout.PAGE_END);
      // mainPanel.setPreferredSize(new Dimension(550, 400));
      JFrame frame = new JFrame();
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.setSize(550, 400);
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }
}

Output

Code Explanation

In this program, we first create a JButton with the label "This is Demo Text!" and customize its appearance by setting its background color to blue and the text color to white. 

We have created a button component here, which will placed in the bottom-right corner ?

JButton button = new JButton("This is Demo Text!");
button.setBackground(Color.blue);
button.setForeground(Color.white);

To place the button in the bottom-right corner, we create a JPanel using the BorderLayout layout manager and add the button to the LINE_END position, which represents the right side of the panel. Then, we create another JPanel, also with BorderLayout, and add the previous panel (containing the button) to the PAGE_END, which is the bottom of the panel. 

JPanel bottomPanel = new JPanel(new BorderLayout());
bottomPanel.add(button, BorderLayout.LINE_END);
JPanel mainPanel = new JPanel(new BorderLayout());
mainPanel.add(bottomPanel, BorderLayout.PAGE_END);

Finally, we set up the JFrame, added the main panel to the frame, and adjusted the frame's size and visibility. When run, the button is displayed in the bottom-right corner of the window.

Updated on: 2024-09-19T21:57:30+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements