Java Reference
In-Depth Information
Table 4-8.
JLabel Properties (Continued)
Property Name
Data Type
Access
icon
Icon
Read-write bound
iconTextGap
int
Read-write bound
labelFor
Component
Read-write bound
text
String
Read-write bound
UI
LabelUI
Read-write
UIClassID
String
Read-only
verticalAlignment
int
Read-write bound
verticalTextPosition
int
Read-write bound
The content of the
JLabel
is the text and its associated image. Displaying an image within
a
JLabel
will be discussed in the “Interface Icon” section later in this chapter. However, you can
display different icons, depending on whether the
JLabel
is enabled or disabled. By default, the
icon is a grayscaled version of the enabled icon, if the enabled icon comes from an
Image
object
(
ImageIcon
, as described later in the chapter). If the enabled icon doesn't come from an
Image
,
there's no icon when
JLabel
is disabled, unless manually specified.
The position of the contents of the
JLabel
is described by four different properties:
horizontalAlignment
,
horizontalTextPosition
,
verticalAlignment
, and
verticalTextPosition
.
The
horizontalAlignment
and
verticalAlignment
properties describe the position of the contents
of the
JLabel
within the container in which it's placed.
■
Note
Alignments have an effect only if there's extra space for the layout manager to position the compo-
nent. If you're using a layout manager such as
FlowLayout
, which sizes components to their preferred size,
these settings will effectively be ignored.
The horizontal position can be any of the
JLabel
constants
LEFT
,
RIGHT
, or
CENTER
. The
vertical position can be
TOP
,
BOTTOM
, or
CENTER
. Figure 4-7 shows various alignment settings,
with the label reflecting the alignments.
The text position properties reflect where the text is positioned relative to the icon when
both are present. The properties can be set to the same constants as the alignment constants.
Figure 4-8 shows various text position settings, with each label reflecting the setting.
■
Note
The constants for the different positions come from the
SwingConstants
interface that the
JLabel
class implements.