Java Reference
In-Depth Information
Figure 17-9
shows how the dimensions relate to the font.
The
advancewidth
for a character is the distance from the reference point of the character to the reference
point of the next character. The
reference point
for a character is on the baseline at the left edge of the char-
acter. Each character has its own advance width, which you can obtain by calling a
FontMetrics
method
charWidth()
. For example, to obtain the advance width for the character
'X'
, the following statement could
be used:
int widthX = metrics.charWidth('X');
You can also obtain the advance widths for the first 256 characters in the font as an array of type
int
with
the method
getWidths()
:
int[] widths = metrics.getWidths();
If you just want the maximum advance width for the characters in the font, you can call the method
getMaxAdvance()
. Lastly, you can get the total advance width for a
String
object by passing the object
to the method
stringWidth()
. The advance width is returned as a value of type
int
. It's important to ap-
preciate that the advance width for a string is not necessarily the sum of the widths of the characters that it
contains.
Although you now know a great deal about how to create and manipulate fonts, you haven't actually cre-
ated and used one. You remedy this after you get a feel for what Swing components can do and have learned
a little about using containers.
Swing Components
Swing components all have the
JComponent
class as a base, which itself extends the
Component
class to add
the following capability:
• Support for pluggable look-and-feel for components, allowing you to change the look-and-feel
programmatically, or implement your own look-and-feel for all components displayed.
• Support for tooltips — a
tooltip
being a message describing the purpose of a component when the
mouse cursor lingers over it. Tooltips are defined by the
JToolTip
class.
• Support for automatic scrolling in a list, a table, or a tree when a component is dragged with the
mouse.
• Component class extensions to enable you to create your own custom components.