The JComponent Class
Most Swing components
are implemented as subclasses of the
JComponent
class,
which inherits from the
Container
class.
From JComponent,
Swing components inherit the following functionality:
- Borders.
- Using the
setBorder method,
you can specify the border
that a component displays around its edges.
You can specify that a component have extra space
around its edges
using an EmptyBorder instance.
See the
BorderFactory
specification
and
Understanding Borders
(an article in The Swing Connection) for more information.
- Double buffering.
- Double buffering can improve the appearance
of a frequently changing component.
Now you don't have to write the double buffering code --
Swing provides it for you.
By default, Swing components are double buffered.
By invoking
setDoubleBuffered(false) on a component,
you turn off its double buffering.
- Tool tips.
- By specifying a string
with the
setToolTipText method,
you can provide help to users of a component.
When the cursor pauses over the component,
the specified string is displayed in a small window
that appears near the component.
See How to Use Tool Tips
for more information.
- Keyboard navigation.
- Using the
registerKeyboardAction method,
you can enable the user
to use the keyboard, instead of the mouse,
to maneuver through the GUI
Note:
Some classes provide convenience methods for keyboard actions.
For example, AbstractButton provides
setMnemonic,
which lets you specify the character that,
in combination with a look-and-feel-specific modifier key,
causes the button's action to be performed.
See How to Use Buttons, Check Boxes, and Radio Buttons
for an example of using mnemonics in buttons.
The combination of character and modifier keys
that the user must press to start an action
is represented by a
KeyStroke
object.
The resulting action event
is handled by an
ActionListener
object.
Each keyboard action works under exactly one of two
conditions:
either when the actual component has the focus
or when any component in its containing window has the focus.
- Properties.
- With the
putProperty method,
you can associate one or more properties
(name/object pairs)
with any JComponent.
For example,
a layout manager might use properties
to associate a constraints object
with each JComponent it manages.
You put and get properties using the
putClientProperty and
getClientProperty methods.
- Application-wide pluggable look and feel.
- Behind the scenes, each
JComponent object
has a corresponding ComponentUI object
that performs all the drawing, event handling, size determination,
and so on for that JComponent.
Exactly which ComponentUI object is used
depends on the current look and feel,
which you can set using the UIManager.setLookAndFeel
method.
- Support for layout.
- With methods such as
setPreferredSize,
setMinimumSize,
setMaximumSize,
setAlignmentX, and
setAlignmentY,
you can specify layout constraints
without having to write your own component.
- Support for accessibility.
- [PENDING: describe]
- Support for localization.
- [PENDING: describe]