Swing Borders

Border is an interface describing an object capable of rendering a border around the edge of a host component. In the Swing component set, borders supercede Inset objects as the mechanism for creating a (decorated or plain) area around the edge of a component.

Border instances are designed to be shared. Rather than creating a new border object by calling new directly on a border class, use the BorderFactory static methods, which produce a shared instance of the common border types.

EmptyBorder is the most trivial border - it just allocates empty space around the edge of its host component. The example creates an EmptyBorder when "CompoundEmptyXxx" is selected (the "Margin" field is used to configure its width).

LineBorder draws a simple rectangle around the host component using the specified color and thickness. In the example, LineBorder is drawn inside an EmptyBorder when "CompoundEmptyOut" is selected, and outside the EmptyBorder when "CompoundEmptyIn" is selected.

EtchedBorder draws a 3D, 2-pixel-wide, raised groove around the edge of its host component. BorderFactory has methods for specifying lowered grooves and custom colors, but they are not exercised in the example.

BevelBorder is similar to EtchedBorder, except that the colors are used to create the impression that the region inside the border is raised above or depressed into the component's surface.

CompoundBorder composes two other borders (one, or both, could be another CompoundBorder). The "CompoundDouble" option in the example draws a LineBorder border inside of an EmptyBorder inside of another LineBorder.

The JTextField and the bottom two JComboBoxes configure a TitledBorder. Use the example to explore permutations of "title", "justification", and "position".

When the example is run, make the window bigger so that the JLabel object and its border(s) don't get clipped. As options are specified, select the JButton to see the results.

[Reference: Topley, pp573-582]