|
|
Using the JFC/Swing Packages |
This section has general information on what Swing components have in common. It tells you about features that theJComponentclass provides to most Swing components, including code on how to use these features.What the
JComponentClass ProvidesMost Swing components are implemented as subclasses of theJComponentclass, which inherits from the
Componentclass. FromJComponent, Swing components inherit the following functionality:
- Borders
- Double buffering
- Tool tips
- Keyboard-generated actions
- Support for scrolling
- Properties
- Application-wide pluggable look and feel
- Support for layout
- Borders.
- Using the
setBordermethod, 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 anEmptyBorderinstance. See [x-ref].
- 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 invoking
setDoubleBuffered(true)on a component, you make it double buffered.
- Tool tips.
- By specifying a string with the
setToolTipmethod, 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-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,AbstractButtonprovidessetKeyAccelerator, 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 aJActionobject. 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
computeVisibleRectandgetVisibleRectmethods, you can determine what part of a component might be visible onscreen. This is especially handy for improving the performance of scrolling. [CHECK] ThescrollRectToVisiblemethod sends a message to theJComponent's parent, requesting that it display the specified area. With thesetAutoscrollsmethod, you can specify that a component should automatically scroll when dragged, if its container is one (such asJViewport) that supports scrolling.
- Properties.
- With the
putPropertymethod, 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 eachJComponentit manages. You get properties using thegetPropertymethod.
- 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 theUIManager.setUIFactorymethod. Behind the scenes, eachJComponentobject has a correspondingComponentUIobject (created by theUIFactory) that performs all the drawing, event handling, size determination, and so on for thatJComponent.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
setPreferredSize,setMinimumSize,setMaximumSize,setAlignmentX, andsetAlignmentY, you can specify layout constraints without having to write your own component.
|
|
Using the JFC/Swing Packages |