Java Notes

Borders

Where to use borders

Best used with JPanel. Borders are commonly used in two situations:

Avoid with other components and graphics JPanels.

Good with JPanels, not good with other components, impossible with Containers

Method setBorder(). The setBorder() method is defined in the JComponent class, and is therefore inherited by all child classes, which basically means all swing components, most importantly JPanel.

Best on JPanels, not other JComponents. Borders are most often added to JPanels, and somewhat less often to other JComponents, such as JLabels. Although you can add a border to any JComponent, as a practical matter they often don't display well. If you need to add a border to a component, put the component in a JPanel and add the border to that JPanel.

Creating borders

Creating borders. To create borders, use factory methods in the javax.swing.BorderFactory class. This and other border classes can be used with

import javax.swing.border.*;

Reuse. A Border object describes how to draw the border. The same Border object may be reused to set the border of many components.

Borders can be grouped into these types:

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 content = new JPanel();
content.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));

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");
controls.setBorder(titled);
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 may get a more attractive effect if you use a compound border, a border that is created by combining two or more borders.

Border inner = BorderFactory.create . . .
Border outer = BorderFactory.create . . .
Border combined = BorderFactory.createCompoundBorder(outer, inner);

p.setBorder(combined);

Problem: Border insets on graphics panel

Insets. When you add a border to a JPanel or JComponent you are using for graphics, you need to know the amount of space the borders use on the panel in order to paint it correctly. For this reason it is probably best not to add a border to a graphics panel, but instead put the graphics panel inside another panel which does have the border.

To paint a bordered 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:

@Override public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Insets ins = this.getInsets();
    int h = this.getHeight() - ins.top - ins.bottom;
    int w = this.getWidth() - ins.left - ins.right;
    g.fillRect(ins.left, ins.top, w, h);
}