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.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(300, 200);

        JPanel panel = new JPanel();
        panel.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
        panel.setLayout(new BorderLayout());
        panel.setSize(200, 100);

        JLabel label = new JLabel("Hello, World!");
        label.setHorizontalAlignment(JLabel.CENTER);
        label.setVerticalAlignment(JLabel.CENTER);
        panel.add(label);

        frame.add(panel, BorderLayout.NORTH);
        frame.setVisible(true);
    }
}

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.

Output:

Bevelborder in Java swing

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

  • RAISED
  • LOWERED

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.

Reference:

  • https://docs.oracle.com/javase/7/docs/api/javax/swing/border/BevelBorder.html