Java Reference
In-Depth Information
Note
EXIT_ON_CLOSE
was introduced into the
JFrame
class in Java 1.3, and
subsequently added to
WindowsConstants
in Java 1.4 (for completeness).
New Lightweight Components and Containers
Swing's lightweight components and containers are implemented by subclasses of the
abstract
javax.swing.JComponent
class, which extends
Container
. (I previ-
ouslymentionedthatcomponentsandcontainerscreatedfromcustom
Component
and
Container
subclasses are known as lightweight components and containers.) They
donothavepeersbutreusethepeersoftheir closest heavyweight ancestors. Afterall,
Swing must eventually ensure that the platform's windowing system can display them.
JComponent
introduces several new features, including tooltips, borders, and the
option of creating nonrectangular components:
• A
tooltip
isasmall(typicallyrectangular)windowappearingoveracomponent
with a small amount of help text.
JComponent
declares a
void
setToolTipText(String text)
methodforspecifyingthecomponent's
tooltip text.
• A
border
isanobjectthatsitsbetweenaSwingcomponent'sedgesandthatof
itscontainer.
JComponent
declaresa
void setBorder(Border bor-
der)
method for setting the border to
border
, which is an instance of a
class that implements the
javax.swing.border.Border
interface. The
javax.swing.BorderFactory
class declares several class methods for
returning different kinds of borders. For example,
Border cre-
ateEtchedBorder(int type)
createsanetchedborderbyinstantiating
the
javax.swing.border.EtchedBorder
class. The argument passed
to
type
must be one of
EtchedBorder.RAISED
or
EtchedBorder.LOWERED
.
• Predefined AWT components (such as buttons) are rectangular because their
nativescreenresourcesarerectangular.Whenyoucreateyourowncomponents
(bysubclassing
JComponent
),youcanmakethemnonrectangularbypassing
false
to
JComponent
's
void setOpaque(boolean isOpaque)
method, which indicates that not every pixel is painted (so background pixels
canshowthrough).Passing
true
tothismethodindicatesthatthecomponent
paints every pixel. (The default value is false.)
I'll demonstrate tooltips and borders later in this chapter.