How can we implement different borders using the BorderFactory in Java?

AWTJavaObject Oriented ProgrammingProgrammingSwing

The BorderFactory is a Factory class which provides different types of borders in Java.

Types of Borders

  • BevelBorder: This border draws raised or lowered beveled edges.
  • EmptyBorder:  It doesn’t do any drawing, but does take up space.
  • EtchedBorder: A Lowered etched border gives an appearance of a rectangle and a Raised etched border looks like a surface of the screen.
  • LineBorder: Draws a simple rectangle around a component. We can specify the color and width of the line in the LineBorder constructor.
  • MatteBorder: We can create a MatteBorder with a certain color and specify the size of the border on the left, top, right, and bottom of the component. A MatteBorder also allows us to pass an Icon that will be used to draw the border. This can be an image (ImageIcon) or any other implementation of the Icon interface.
  • TitledBorder: A regular border with a title. A TitledBorder doesn’t actually draw a border; it just draws a title in conjunction with another border object. This border type is particularly useful for grouping different sets of controls in a complicated interface.
  • Component Border: A border that contains two other borders. This is especially handy if we want to enclose a component in an EmptyBorder and then put something decorative around it, such as an EtchedBorder or a MatteBorder.


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
public class BorderFactoryMain {
   public static void main(String[] args) {
   static Runnable run = new Runnable() {
      public void run() {
         BorderFactoryTest test;
         test = new BorderFactoryTest();
   public static class BorderFactoryTest extends JFrame {
      public BorderFactoryTest() {
         setTitle("BorderFactory Test");
         setSize(350, 400);
         setLayout(new FlowLayout());
         add(createBorderedPanel(BorderFactory.createRaisedBevelBorder(),       "createRaisedBevelBorder()"));
         add(createBorderedPanel(BorderFactory.createBevelBorder(BevelBorder.LOWERED), "createBevelBorder(BevelBorder.LOWERED)"));
         add(createBorderedPanel(BorderFactory.createBevelBorder(BevelBorder.RAISED), "createBevelBorder(BevelBorder.RAISED)"));
         "createCompoundBorder(RAISED, LOWERED)"));
         add(createBorderedPanel(BorderFactory.createEtchedBorder(), "createEtchedBorder()"));
         add(createBorderedPanel(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED),       "createEtchedBorder(EtchedBorder.LOWERED)"));
         add(createBorderedPanel(BorderFactory.createEtchedBorder(EtchedBorder.RAISED), "createEtchedBorder(EtchedBorder.RAISED)"));
         add(createBorderedPanel(BorderFactory.createEtchedBorder(Color.lightGray, Color.yellow), "createEtchedBorder(Color.lightGray, Color.yellow)"));
         add(createBorderedPanel(BorderFactory.createLineBorder(, "createLineBorder("));
         add(createBorderedPanel(BorderFactory.createLineBorder(, 5), "createLineBorder(, 5)"));
         add(createBorderedPanel(BorderFactory.createDashedBorder(null), "createDashedBorder(null)"));
   private static JPanel createBorderedPanel(Border b, String name) {
      JLabel label = new JLabel(name);
      JPanel panel = new JPanel();
      return panel;


Published on 09-Jul-2019 17:01:40