Using Swing Components |
Most Swing components are implemented as subclasses of theJComponent
class, which inherits from theContainer
class. FromJComponent
, Swing components inherit the following functionality:
- Borders
- Double buffering
- Tool tips
- Keyboard-generated actions
- Properties
- Application-wide pluggable look and feel
- Support for layout
- 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 anEmptyBorder
instance. See theBorderFactory
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 GUIThe combination of character and modifier keys that the user must press to start an action is represented by a
Note: Some classes provide convenience methods for keyboard actions. For example,AbstractButton
providessetMnemonic
, 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.
KeyStroke
object. The resulting action event is handled by anActionListener
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 anyJComponent
. For example, a layout manager might use properties to associate a constraints object with eachJComponent
it manages. You put and get properties using theputClientProperty
andgetClientProperty
methods.
- Application-wide pluggable look and feel.
- Behind the scenes, each
JComponent
object has a correspondingComponentUI
object that performs all the drawing, event handling, size determination, and so on for thatJComponent
. Exactly whichComponentUI
object is used depends on the current look and feel, which you can set using theUIManager.setLookAndFeel
method.
- Support for layout.
- With methods such as
setPreferredSize
,setMinimumSize
,setMaximumSize
,setAlignmentX
, andsetAlignmentY
, you can specify layout constraints without having to write your own component.
- Support for accessibility.
- [PENDING: describe]
- Support for localization.
- [PENDING: describe]
Using Swing Components |