BevelBorder in Java Swing

BevelBorder is a border style in Java Swing that creates a raised or lowered beveled edge around a component. It is a subclass of the Border class and can be applied to any Swing component that supports borders.

The BevelBorder class has several constructors that allow us to customize the appearance of the border.

Following is an example of creating a BevelBorder with a raised edge:

import java.awt.BorderLayout;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.BevelBorder;

public class BevelBorderDemo {
	public static void main(String[] args) {
        JFrame frame = new JFrame("BevelBorder Demo");
        frame.setSize(300, 200);

        JPanel panel = new JPanel();
        panel.setLayout(new BorderLayout());
        panel.setSize(200, 100);

        JLabel label = new JLabel("Hello, World!");

        frame.add(panel, BorderLayout.NORTH);

In this example, a JPanel is created and a BevelBorder with a raised edge is applied to it using the createBevelBorder() method of the BorderFactory class. The JLabel “Hello, World!” is then added to the panel and centered within it. The panel is positioned at the top of the frame with the help of BorderLayout.NORTH.


Bevelborder in Java swing
BevelBorder in Java Swing 2

There are two different styles of BevelBorder that we can choose from:


In addition to the style, we can also specify the colors of the border using the following constructors:

BorderFactory.createBevelBorder(int type, Color highlight, Color shadow)
BorderFactory.createBevelBorder(int type, Color highlightOuter, Color highlightInner, Color shadowOuter, Color shadowInner)

The first constructor takes two colors: highlight and shadow, which are used for the raised and lowered edges, respectively. The second constructor takes four colors: highlightOuter, highlightInner, shadowOuter, and shadowInner, which are used for the four edges of the border.