- 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 a JButton in Java?
To add empty border to a component, use the BorderFactory class createEmptyBorder() method −
Border emptyBorder = BorderFactory.createEmptyBorder(10, 10, 0, 0);
To set the above border to a component, use the setBorder() method −
JButton button = new JButton("Empty Border"); button.setBorder(emptyBorder);
The following is an example to ad empty border to a JButton −
Example
package my; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Container; import javax.swing.BorderFactory; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.border.Border; import javax.swing.border.SoftBevelBorder; public class SwingDemo { public static void main(String args[]) { JFrame frame = new JFrame("Demo"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Border raisedBorder = new SoftBevelBorder(SoftBevelBorder.RAISED, Color.GREEN, Color.GREEN.darker(), Color.MAGENTA, Color.magenta.brighter()); Border raisedBorder2 = new SoftBevelBorder(SoftBevelBorder.RAISED); Border loweredBorder = new SoftBevelBorder(SoftBevelBorder.LOWERED, Color.ORANGE, Color.YELLOW.darker(), Color.BLUE, Color.yellow.brighter()); Border emptyBorder = BorderFactory.createEmptyBorder(10, 10, 0, 0); JButton raisedButton = new JButton("Raised Border"); raisedButton.setBorder(raisedBorder); JButton loweredButton = new JButton("Lowered Border"); loweredButton.setBorder(loweredBorder); JLabel raisedLabel = new JLabel("Raised Border"); raisedLabel.setBorder(raisedBorder2); JButton button = new JButton("Empty Border"); button.setBorder(emptyBorder); Container contentPane = frame.getContentPane(); contentPane.add(raisedButton,BorderLayout.WEST); contentPane.add(loweredButton,BorderLayout.EAST); contentPane.add(raisedLabel,BorderLayout.CENTER); contentPane.add(button,BorderLayout.NORTH); frame.setSize(600, 300); frame.setVisible(true); } }
Output
- Related Articles
- How to add empty border to JPanel in Java?
- How to add Icon to JButton in Java?
- How to add action listener to JButton in Java
- How can we add/insert a JButton to JTable cell in Java?
- How to add line border to JLabel in Java?
- How to add a title to an existing line border in Java?
- How to set action command to JButton in Java
- How to change JButton font dynamically in Java?
- How to implement a rollover effect for a JButton in Java?
- How to create and set an Empty Border from BorderFactory class in Java?
- How can we set the margin to a JButton in Java?
- Java Program to add Titled Border to Panel in Swing
- Create empty border with BorderFactory class in Java?
- How can we set the shortcut key to a JButton in Java?
- How can we apply different borders to JButton in Java?

Advertisements