Java Swing Tutorial - Java Swing Borders








Swing allows us to draw a border around the edges of components. There are different kinds of borders:

  • Bevel Border
  • Soft Bevel Border
  • Etched Border
  • Line Border
  • Titled Border
  • Matte Border
  • Empty Border
  • Compound Border

Border is an interface that is implemented by all classes whose instances represent a specific kind of border. There is one class for each kind of border.

We can create a custom border by inheriting a class from the AbstractBorder class.

All border-related classes and the Border interface are in the javax.swing.border package.

javax.swing.BorderFactory class can create a border. and it takes care of caching and sharing of border objects.

The following code creates different kinds of borders.

To create bevel borders

Border  bevelRaisedBorder = BorderFactory.createBevelBorder(BevelBorder.RAISED); 
Border  bevelLoweredBorder = BorderFactory.createBevelBorder(BevelBorder.LOWERED);

To create soft bevel borders

Border  softBevelRaisedBorder = BorderFactory.createSoftBevelBorder(BevelBorder.RAISED); 
Border  softBevelLoweredBorder = BorderFactory.createSoftBevelBorder(BevelBorder.LOWERED);

To create etched borders

Border  etchedRaisedBorder = BorderFactory.createEtchedBorder(EtchedBorder.RAISED); 
Border  etchedLoweredBorder = BorderFactory.createEtchedBorder(EtchedBorder.LOWERED);

To create line borders

Border  lineBorder = BorderFactory.createLineBorder(Color.BLACK);
Border  lineThickerBorder = BorderFactory.createLineBorder(Color.BLACK, 3);

To 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);

To create a matte border

Border  matteBorder = BorderFactory.createMatteBorder(1,3,5,7,  Color.BLUE);

To create an empty border

Border  emptyBorder  = BorderFactory.createEmptyBorder();

To create compound borders

Border  twoCompoundBorder = BorderFactory.createCompoundBorder(etchedRaisedBorder, lineBorder);
Border  threeCompoundBorder = BorderFactory.createCompoundBorder(titledBorderAtTop,  twoCompoundBorder);

To set a border to a component

myComponent.setBorder(matteBorder);

A bevel border gives we a three-dimensional effect by using shadows and highlights to the inside and outside edges of the border. We can have a raised or lowered effect. A soft bevel border is a bevel border with softer corners.

An etched border gives we a carved effect. It can be raised and lowered.

A line border simply draws a line. We can specify the color and thickness of the line.

The title of a border is text that can be displayed at a specified position in the border, such as in the middle of the top/bottom border or above top/below bottom.

We can specify the justification of the title text, its color, and font.

We must have another border object to use a title border. A title border just lets we supply the title text to another kind of border.

A matte border decorates a border with an icon.

If we do not have an icon, we can specify the border's thickness.

An empty border doesn't display anything. To add spaces around a component, we can use an empty border. An empty border lets we specify the spacing to be used for all four sides separately.

A compound border is a composite border that combines any two kinds of borders into one border object.