Java Reference
In-Depth Information
Examine Figure 7-1 to see a sampling of the various border configurations around a
JLabel
,
with a text label designating the border type. How to create the different borders will be discussed
in later sections of this chapter.
Figure 7-1.
Border examples, using a 4-by-2 GridLayout with 5-pixel horizontal and vertical gaps
Exploring the Border Interface
The
Border
interface can be found in the
javax.swing.border
package. This interface forms the
basis of all the border classes. The interface is directly implemented by the
AbstractBorder
class, which is the parent class of all the predefined Swing border classes:
BevelBorder
,
CompoundBorder
,
EmptyBorder
,
EtchedBorder
,
LineBorder
,
MatteBorder
,
SoftBevelBorder
, and
TitledBorder
. Of additional interest is the
BorderFactory
class, found in the
javax.swing
package. This class uses the Factory design pattern to create borders, hiding the details of the
concrete implementations and caching various operations to optimize shared usages.
The
Border
interface shown here consists of three methods:
paintBorder()
,
getBorderInsets()
, and
isBorderOpaque()
. These methods are described in the following
sections.
paintBorder()
The
paintBorder()
method is the key method of the interface. It has the following definition:
public void paintBorder(Component c, Graphics g, int x, int y, int
width, int height)
The actual drawing of the border is done in this method. Frequently, the
Border
imple-
mentation will ask for the
Insets
dimensions first, and then draw the border in the four
rectangular outer regions, as shown in Figure 7-2. If a border is opaque, the
paintBorder()
implementation must fill the entire insets area. If a border is opaque and doesn't fill the area,
then it's a bug and needs to be corrected.