Java examples for Swing:Border
Swing draws a border around the edges of components.
There are different kinds of borders:
Available Border Classes
Type of Border | Border Class |
---|---|
Bevel Border | BevelBorder |
Soft Bevel Border | SoftBevelBorder |
Etched Border | EtchedBorder |
Line Border | LineBorder |
Titled Border | TitledBorder |
Matte Border | MatteBorder |
Empty Border | EmptyBorder |
Compound Border | CompoundBorder |
The following snippet of code creates different kinds of borders:
import java.awt.Color; import javax.swing.BorderFactory; import javax.swing.JButton; import javax.swing.border.BevelBorder; import javax.swing.border.Border; import javax.swing.border.EtchedBorder; import javax.swing.border.TitledBorder; public class Main { public static void main(String[] args) { // Create bevel borders Border bevelRaisedBorder = BorderFactory .createBevelBorder(BevelBorder.RAISED); Border bevelLoweredBorder = BorderFactory .createBevelBorder(BevelBorder.LOWERED); // Create soft bevel borders Border softBevelRaisedBorder = BorderFactory .createSoftBevelBorder(BevelBorder.RAISED); Border softBevelLoweredBorder = BorderFactory .createSoftBevelBorder(BevelBorder.LOWERED); // Create etched borders Border etchedRaisedBorder = BorderFactory .createEtchedBorder(EtchedBorder.RAISED); Border etchedLoweredBorder = BorderFactory .createEtchedBorder(EtchedBorder.LOWERED); // Create line borders Border lineBorder = BorderFactory.createLineBorder(Color.BLACK); Border lineThickerBorder = BorderFactory.createLineBorder(Color.BLACK, 3); // Create titled borders Border titledBorderAtTop = BorderFactory.createTitledBorder( etchedLoweredBorder, "Title text goes here", TitledBorder.CENTER, TitledBorder.TOP); Border titledBorderAtBottom = BorderFactory.createTitledBorder( etchedLoweredBorder, "Title text goes here", TitledBorder.CENTER, TitledBorder.BOTTOM); // Create a matte border Border matteBorder = BorderFactory .createMatteBorder(1, 3, 5, 7, Color.BLUE); // Create an empty border Border emptyBorder = BorderFactory.createEmptyBorder(); // Create compound borders Border twoCompoundBorder = BorderFactory.createCompoundBorder( etchedRaisedBorder, lineBorder); Border threeCompoundBorder = BorderFactory.createCompoundBorder( titledBorderAtTop, twoCompoundBorder); new JButton().setBorder(matteBorder); } }