- 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 to add empty border to JPanel in Java?
To add empty border, use the createEmtyBorder() method. Let us first create a new JLabel −
JLabel label; label = new JLabel("Label with empty border!");
Now, set empty border using the setBorder() method −
label.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
The following is an example to add empty border to JPanel −
Example
package my; import javax.swing.BorderFactory; import java.awt.Font; import javax.swing.JFrame; import javax.swing.JLabel; public class SwingDemo { public static void main(String[] args) throws Exception { JFrame frame = new JFrame("Demo"); JLabel label; label = new JLabel("Label with empty border!"); label.setFont(new Font("Verdana", Font.PLAIN, 16)); label.setVerticalAlignment(JLabel.BOTTOM); label.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10)); frame.add(label); frame.setSize(550,350); frame.setVisible(true); } }
Output
- Related Articles
- How to add empty border to a JButton in Java?
- How to add line border to JLabel in Java?
- How to create and set an Empty Border from BorderFactory class in Java?
- How to add a title to an existing line border in Java?
- Java Program to add Titled Border to Panel in Swing
- Create empty border with BorderFactory class in Java?
- How to center a JLabel in a JPanel with GridBagLayout in Java?
- How can we set the background color to a JPanel in Java?
- How to add space around table border in HTML?
- How can we implement a scrollable JPanel in Java?
- How to add a border to an image with CSS?
- Java Program to center a JLabel in a JPanel with LayoutManager
- How to empty an array in Java
- How to add a colored border to a button with CSS?
- How can I set a table in a JPanel in Java?

Advertisements