- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How can we implement different borders using the BorderFactory in Java?n
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
- Related Articles
- How can we apply different borders to JButton in Java?
- How can we Implement a Stack using Queue in Java?
- How can we implement right click menu using JPopupMenu in Java?
- How can we Implement a Queue using Stack in Java?\n
- How can we implement a JLabel text with different color and font in Java?
- How can we implement a JToggleButton in Java?
- How can we implement editable JComboBox in Java?
- How can we implement transparent JDialog in Java?
- How can we implement a JSON array using Streaming API in Java?
- How can we implement Flow API using Publisher-Subscriber in Java 9?
- How can we implement a splash screen using JWindow in Java?\n
- How can we implement a moving text using a JLabel in Java?
- How can we implement the SubmissionPublisher class in Java 9?
- How can we implement the Subscriber interface in Java 9?
- How can we implement a Custom HashSet in Java?

Advertisements