Java Reference
In-Depth Information
frame.setSize(300, 100);
frame.setVisible(true);
}
};
EventQueue.invokeLater(runner);
}
}
Customizing TitledBorder Look and Feel
The available set of
UIResource
-related properties for a
TitledBorder
is shown in Table 7-4.
It has three different properties.
Table 7-4.
TitledBorder UIResource Elements
Property String
Object Type
TitledBorder.font
Font
TitledBorder.titleColor
Color
TitledBorder.border
Border
Creating Your Own Borders
When you want to create your own distinctive border, you can either create a new class that
implements the
Border
interface directly or you can extend the
AbstractBorder
class. As previ-
ously mentioned, extending the
AbstractBorder
class is the better way to go, because optimizations
are built in to certain Swing classes to take advantage of some of the
AbstractBorder
-specific
methods. For instance, if a border is an
AbstractBorder
,
JComponent
will reuse an
Insets
object
when getting the
Insets
of a border. Thus, one fewer object will need to be created and destroyed
each time the insets are fetched.
In addition to thinking about subclassing
AbstractBorder
versus implementing the
Border
interface yourself, you need to consider whether or not you want a static border. If you attach
a border to a button, you want that button to be able to signal selection. You must examine the
component passed into the
paintBorder()
method and react accordingly. In addition, you
should also draw a disabled border to indicate when the component isn't selectable. Although
setEnabled(false)
disables the selection of the component, if the component has a border
associated with it, the border still must be drawn, even when disabled. Figure 7-16 shows one
border in action that looks at all these options for the component passed into the border's
paintBorder()
method.
Figure 7-16.
Active custom border examples