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


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.

Example

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
public class BorderFactoryMain {
   public static void main(String[] args) {
      SwingUtilities.invokeLater(run);
   }
   static Runnable run = new Runnable() {
      @Override
      public void run() {
         BorderFactoryTest test;
         test = new BorderFactoryTest();
         test.setVisible(true);
      }
   };
   public static class BorderFactoryTest extends JFrame {
      public BorderFactoryTest() {
         setTitle("BorderFactory Test");
         setSize(350, 400);
         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         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)"));
         add(createBorderedPanel(BorderFactory.createCompoundBorder(BorderFactory.
         createBevelBorder(BevelBorder.RAISED),BorderFactory.createBevelBorder(BevelBorder.LOWERED)),
         "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(Color.red), "createLineBorder(Color.red)"));
         add(createBorderedPanel(BorderFactory.createLineBorder(Color.blue, 5), "createLineBorder(Color.blue, 5)"));
         add(createBorderedPanel(BorderFactory.createDashedBorder(null), "createDashedBorder(null)"));
         setLocationRelativeTo(null);
      }
   }
   private static JPanel createBorderedPanel(Border b, String name) {
      JLabel label = new JLabel(name);
      JPanel panel = new JPanel();
      panel.setBorder(b);
      panel.add(label);
      return panel;
   }
}

Output

Updated on: 07-Feb-2020

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements