Java Swing GUI

How to Change the Border of a JFrame in Java

In this tutorial, we are going to see how to change the border of a JFrame in Java. You can draw borders around an undecorated JFrame. Just get the root panel of the JFrame and set its borders with the method setBorder(Border border).

First remove the frame decoration:


Then draw the border:

        BorderFactory.createMatteBorder(4, 4, 4, 4, Color.BLUE)


Java Program to Change the Border of a JFrame:
import java.awt.*;
import javax.swing.*;

public class BorderFrameExample extends JFrame 
   JLabel l = new JLabel("Welcome to StackHowTo!", JLabel.CENTER);
   public BorderFrameExample() {
      //add label to frame
      add(l, BorderLayout.CENTER);
      //remove the decoration
      //set border
         BorderFactory.createMatteBorder(4, 4, 4, 4, Color.BLUE)
   public static void main(String[] args) {
      new BorderFrameExample();


mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

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