Ask Questions?

View Latest Questions



Posted on: July 26, 2006 at 12:00 AM
How to create Bordets?

Java Notes


You can add a border to any component (JComponent), including JPanel, using the component's setBorder(...) method. To create borders, use methods in the BorderFactory class. This and other border classes can be used with
import javax.swing.border.*;
A Border object describes how to draw the border, and the same Border object may be used to set the border of many components.

Borders can be grouped into these types:

  • Empty border -- Empty space around a component.
  • Line border -- Several line styles: plain, etched, or beveled.
  • Titled border -- Text on one of the other borders.
  • Compound borders -- A combination of two borders.
  • Matte border -- Images around a component.

To create an empty border

An empty border creates empty space around a component. Give the int number of pixels of empty space on each side: top, left, bottom, right.
JPanel controls = new JPanel();

To create a line border

There are many styles of lines that can be put around a component. Here are some of the most useful.
Border lineBdr = BorderFactory.createLineBorder(c);
Creates a line with Color c.
Border lineBdr = BorderFactory.createLineBorder(c, w);
Creates a line border int w pixels wide with Color c.
Border etchedBdr = BorderFactory.createEtchedBorder()
An etched border looks like a line carved into the background.
Border etchedBdr = BorderFactory.createEtchedBorder(h, s);
Creates an etched border with highlight Color h and shadow Color s.
Border lowerdBdr = BorderFactory.createLoweredBevelBorder();
Makes the component look like it is below the background.
Border raisedBdr = BorderFactory.createRaisedBevelBorder();
Makes the component look like it is above the background.

To create a titled border

A titled border puts a title on another kind of border.
BorderFactory.createTitledBorder(brdr, title)
Creates a titled border with the title at the top left, on top of the existing border brdr.
import javax.swing.border.*;
. . .
JPanel controls = new JPanel();
Border etched = BorderFactory.createEtchedBorder();
Border titled = BorderFactory.createTitledBorder(etched, "Controls");
BorderFactory.createTitledBorder(brdr, text, just, place, fnt, clr)
Use the simple form above if possible, otherwise this more complex version can be used. See You can control the placement and color of the title with the following parameters.
brdrOne of the other borders (line, etched, blank, ...).
titleThe title string to display.
justThe text justification: TitledBorder.LEFT, TitledBorder.CENTER, TitledBorder.RIGHT, TitledBorder.LEADING, TitledBorder.TRAILING, TitledBorder.DEFAULT_JUSTIFICATION (TitledBorder.LEADING)
placeThe title position: TitledBorder.ABOVE_TOP, TitledBorder.TOP, TitledBorder.BELOW_TOP, TitledBorder.ABOVE_BOTTOM, TitledBorder.BOTTOM, TitledBorder.BELOW_BOTTOM, TitledBorder.DEFAULT_POSITION (TitledBorder.TOP)
fntThe title Font.
clrThe Color of the title.

To create a compound border

You will often get a more attractive effect if you use a compound border, a border that is created by combining two other borders.
Border inner = BorderFactory.create . . .
Border outer = BorderFactory.create . . .
Border combined = BorderFactory.createCompoundBorder(outer, inner);


Border insets on graphics panel

When you add a border to a panel you are using for graphics, you need to know the amount of space the borders use on the panel. Call the panel's getInsets method and use the left, right, top, and bottom fields. For example, to draw a panel that has (or might have) a border:
public void paintComponent(Graphics g) {
    Insets ins = this.getInsets();
    int h = this.getHeight() - - ins.bottom;
    int w = this.getWidth() - ins.left - ins.right;
    g.fillRect(ins.left,, w, h);
Copyleft 2003 Fred Swartz MIT License, Last update: 2002-02-01