|GUI Changes: The AWT Grows Up|
This section has general information on what Swing components have in common. It tells you about features that the
JComponentclass provides to most Swing components, including code on how to use these features. One day, it might also tell you how the code in programs that use Swing components differs from that in programs using heavyweight components.
JComponentClass ProvidesMost Swing components are implemented as subclasses of the
JComponentclass, which inherits from the
Componentclass. [somewhere, link to ui trail's discussion of Component] From
JComponent, Swing components inherit the following functionality:
- Tool tips
- Keyboard-generated actions
- Support for scrolling
- Application-wide pluggable look and feel
- Support for layout
- Tool tips.
- By specifying a [non-null?] string with the
setToolTipmethod, you can provide help to users of a component. When the cursor pauses over the component, the specified string will be displayed in a small window that appears near the component. See How to Use Tool Tips for more information.
- Keyboard-generated actions.
- Using the
registerKeyboardActionmethod, you can enable the user to use the keyboard, instead of the mouse, to indicate that the component's action should occur.The 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,
setKeyAccelerator, 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 for an example of using key accelerators in buttons.
JKeyStrokeobject. The resulting action event is handled by a
JActionobject. 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.
- Support for scrolling.
- With the
getVisibleRectmethods, you can determine what part of a component might be visible onscreen. This is especially handy for improving the performance of scrolling. [CHECK] The
scrollRectToVisiblemethod sends a message to the
JComponent's parent, requesting that it display the specified area. With the
setAutoscrollsmethod, you can specify that a component should automatically scroll when dragged, if its container is one (such as
JViewport) that supports scrolling.
- With the
putPropertymethod, 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
JComponentit manages. You get properties using the
- Application-wide pluggable look and feel.
- Each Java runtime has a
UIFactoryobject that determines the look and feel of that runtime's Swing components. Subject to security restrictions, you can choose the look and feel used by all Swing components by invoking the
UIManager.setUIFactorymethod. Behind the scenes, each
JComponentobject has a corresponding
ComponentUIobject (created by the
UIFactory) that performs all the drawing, event handling, size determination, and so on for that
Note: If you want to change the look and feel of an individual Swing component, rather than the whole set of Swing components, you should reimplement the individual component [point to doc]. To learn about implementing a look-and-feel package, [wait for doc!].
- Support for layout.
- With methods such as
setInsets, you can specify layout constraints without having to write your own component.
Considerations for Using Lightweight Components[repaint/validate considerations?]
To avoid "flashing" when lightweight components are redrawn, you need to put lightweight components in double-buffered containers. The Swing release includes two classes that you can use together to get double buffering:
JPanelclass provides double-buffered lightweight containers. The
Framesimply implements a simple drawing fix that lets
JPanelwork. For more information about flashing, look at Eliminating Flashing.
[Go into how lw components differ from hw components, wrt paint and update?]
|GUI Changes: The AWT Grows Up|