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

AWTJavaObject Oriented ProgrammingProgrammingSwing

#### Java AWT Programming Project

13 Lectures 2 hours

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());
createBevelBorder(BevelBorder.RAISED),BorderFactory.createBevelBorder(BevelBorder.LOWERED)),
"createCompoundBorder(RAISED, LOWERED)"));
setLocationRelativeTo(null);
}
}
private static JPanel createBorderedPanel(Border b, String name) {
JLabel label = new JLabel(name);
JPanel panel = new JPanel();
panel.setBorder(b);
}