
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Implement Different Borders Using BorderFactory in Java
In this article, we will learn to implement different borders using the BorderFactory in Java. While building graphical user interfaces (GUIs) with Java Swing, borders can be useful in structuring and managing various sections of our interface. To make it easier to develop several types of borders, Swing offers the BorderFactory class.
What is a BorderFactory?
The BorderFactory is a Factory class that provides different types of borders in Java BorderFactory, located in the javax.swing package, is a utility class containing static methods to create new Border objects. Because these methods internally manage border instances, you typically get reused border instances whenever possible, which can be both memory-efficient and convenient.
Types of Borders
BevelBorder
The BevelBorder border draws raised or lowered beveled edges. BevelBorder creates a 3D effect, but in a more useful manner, either raised or lowered.
Instantiating a BevelBorder:
// Raised bevel border panel.setBorder(BorderFactory.createRaisedBevelBorder()); // Lowered bevel border panel.setBorder(BorderFactory.createLoweredBevelBorder());
The following are the key characteristics of BevelBorder:
-
Stronger 3D effect than etched border
- Two types: raised and lowered
- Uses highlight/shadow colors for depth
EmptyBorder
The EmptyBorder doesn't do any drawing, but it does take up space. Creates a transparent space around a component without any visible border.
Instantiating an EmptyBorder:
JPanel panel = new JPanel(); // Parameters: top, left, bottom, right (in pixels) panel.setBorder(BorderFactory.createEmptyBorder(10, 15, 10, 15));
The following are the key characteristics of EmptyBorder:
- Adds padding/margin around components
- Doesn't draw any visible border lines
- Useful for component spacing
EtchedBorder
A Lowered etched border gives an appearance of a rectangle and a Raised etched border looks like a surface of the screen.
Instantiating an EtchedBorder:
panel.setBorder(BorderFactory.createEtchedBorder());
The following are the key characteristics of EtchedBorder:
- Gives components an engraved/recessed appearance
-
Two color options (highlight/shadow)
- Common for grouping related controls
LineBorder
A LineBorder draws a simple rectangle around a component. We can specify the color and width of the line in the LineBorder constructor.
Instantiating a LineBorder:
panel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
The following are the key characteristics of LineBorder:
- Single color border
- Adjustable thickness
- Sharp rectangular edges
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.
Instantiating a MatteBorder:
// Solid color matte border (same thickness all sides) panel.setBorder(BorderFactory.createMatteBorder( 2, 2, 2, 2, // top, left, bottom, right thickness Color.GREEN // border color )); // Image-based matte border ImageIcon tileIcon = new ImageIcon("texture.png"); panel.setBorder(BorderFactory.createMatteBorder( 10, 10, 10, 10, // border thickness tileIcon // tiled image ));
The following are the key characteristics of MatteBorder:
- Can use color or image
-
Different thickness on each side
- Useful for decorative borders
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.
Instantiating a TitledBorder:
panel.setBorder(BorderFactory.createTitledBorder("Settings"));
The following are the key characteristics of TitledBorder:
- Can wrap any border type
- Customizable title position and font
- Common for grouping components
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.
Instantiating a Component Border:
// Combine line border with empty padding Border outer = BorderFactory.createLineBorder(Color.RED, 3); Border inner = BorderFactory.createEmptyBorder(5, 5, 5, 5); panel.setBorder(BorderFactory.createCompoundBorder(outer, inner));
The following are the key characteristics of TitledBorder:
- Nests one border inside another
- Creates complex border effects
- Often used with empty borders for padding
Example of All Borders
Below is an example of all the borders separately in a single program in Java:
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; } }