This chapter contains a collection of how-tos to help you use miscellaneous Swing features. You can find source files for all of this chapter's examples on the CD and online:
The preceding URLs take you to an example index that has links to the files required by each example. To go directly to the entry for a particular example add # ExampleName to the URL. Most examples have a "Run" link in the index which executes the example using Java Web Start technology.
-
How to Use Actions ” With Action objects, you can coordinate the state and event handling of two or more components that generate action events. For example, you can use a single Action to create and coordinate a tool-bar button and a menu item that perform the same function.
-
How to Support Assistive Technologies ” Swing components have built-in support for assistive technologies. Your program can provide even better support by following a few rules.
-
How to Use Borders ” Bopaintingrders are very handy for painting lines, titles, and empty space around the edges of components. This section tells you how to add a border to any JComponent .
-
How to Use Drag and Drop and Data Transfer ” Most applications can benefit from the ability to transfer information between components using drag and drop or cut, copy, and paste. This section tells you how to implement these features in your program.
-
How to Use the Focus Subsystem ” Some programs need to manipulate focus; for example, to validate input or change the tab order of components. This section describes some techniques you can use to customize focus in your program.
-
How to Use Icons ” Many Swing components can display icons. Usually, icons are implemented as instances of the ImageIcon class.
-
How to Use Key Bindings ” With key bindings, you can specify how components react to a user 's input.
-
How to Set the Look and Feel ” You can specify the look and feel of Swing components.
-
How to Use Threads ” Read this section to learn about using threads properly in programs that use the Swing components. This section talks about the event-dispatching thread and explains how to use methods , such as invokeLater , and classes, such as SwingWorker . It also tells you when you might want to use the Timer class.
-
How to Use Timers ” With the Swing Timer class, you can implement a thread that performs an action after a delay and optionally continues to repeat the action. The action executes in the event-dispatching thread.
If you encounter any problems in this chapter, you may find the solutions in Solving Common Problems Using Other Swing Features (page 743) in the Appendix.
How to Use Actions
The Action API
Examples That Use Actions
How to Support Assistive Technologies
Rules for Supporting Accessibility
Testing for Accessibility
Setting Accessible Names and Descriptions on Components
How Accessibility Works
Making Custom Components Accessible
The Accessibility API
Examples That Use the Accessibility API
How to Use Borders
The BorderDemo Example
Using the Borders Provided by Swing
Creating Custom Borders
The Border API
Examples That Use Borders
How to Use Drag and Drop and Data Transfer
A Visual Guide to Drag-and-Drop Cursor Icons
Introduction to Data Transfer Support
A Simple Example: Adding DnD to JLabel
Extending Default DnD Support
Specifying the Data Format
Importing a New Flavor: Color
Replacing Default Support: Color and Text
Importing a New Flavor: Files
Data Transfer with a Custom Component
Data Transfer with a Custom DataFlavor
Adding Cut/Copy/Paste Support
The Data Transfer API
Examples That Use Data Transfer
How to Use the Focus Subsystem
Introduction to the Focus Subsystem
Validating Input
Making a Custom Component Focusable
Customizing Focus Traversal
Tracking Focus Changes to Multiple Components
Timing Focus Transfers
The Focus API
Examples That Use Focus
How to Use Icons
A More Complex Image Icon Example
Loading Images Using getResource
Loading Images into Applets
Improving Perceived Performance When Loading Image Icons
Creating a Custom Icon Implementation
The Image Icon API
Examples That Use Icons
How to Use Key Bindings
How Key Bindings Work
How to Make and Remove Key Bindings
The Key Binding API
Examples That Use Icons
How to Set the Look and Feel
Programmatically Setting the Look and Feel
Specifying the Look and Feel: Command Line
Specifying the Look and Feel: swing.properties
How the UI Manager Chooses the Look and Feel
Changing the Look and Feel after Startup
How to Use Threads
The Event-Dispatching Thread
Using the invokeLater Method
Using the invokeAndWait Method
Using Threads to Improve Performance
Using the SwingWorker Class
How to Use Timers
The Timer API
Examples That Use Timers