Java Reference
In-Depth Information
The diagram shows how the dimensions relate to the font. The
advance
width
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 base line at the left edge of the character. Each character will
have 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 all the characters in the font as an array of type
int
with
the method
getWidths()
:
int[] widths = metrics.getWidths();
The numerical value for the character is used to index the array, so you can get the advance width for
the character '
X
' with the expression
widths['X']
. 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
.
Although you now know a great deal about how to create and manipulate fonts, we haven't actually
created and used one. We will remedy this after we have got a feel for what Swing components can do
and 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:
Supports 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.
❑