How to create Borders in Java

To create borders around components, Java provides the javax.swing.JComponent.setBorder method. This method requires a border object as a parameter; the border object itself is provided by the javax.swing.BorderFactory class. There are many methods in this class; we will list just a few.

createLineBorder(color) creates a line border with the specified color around the component. For example, the statement

button1.setBorder(BorderFactory.createLineBorder(Color. red));

creates a red line border around the JButton object button1. Note that to use the Color.red object, we would need the following statement in the application code:

import java.awt.Color;

createLoweredBevelBorder() and createRaisedBevelBorder() create borders with lowered and raised bevel edges. For example,

label.setBorder(BorderFactory.createLoweredBevelBorder());

creates a lowered bevel border for the JLabel object label.

No comments:

Post a Comment