Chapter 21. Carets, Highlighters, and Keymaps


Like some of the other Swing components (JTree, for example), the text components allow you to do a certain amount of customization without having to implement your own L&F. Certain aspects of these components' behavior and appearance can be modified directly through properties of JTextSCComponent. This chapter explains how to modify three such components: carets, highlighters, and keymaps.

With the more flexible text components (JEditorPane and anything that extends it, including JTextPane), you can control the View objects created to render each Element of the Document model. In this chapter, we'll concentrate on the classes and interfaces related to modifying text components without dealing with View objects. Chapter 23 discusses custom View classes.

JTextComponent has three UI-related properties that you can access and modify directly. These properties are defined by the following interfaces:

Caret

Keeps track of where the insertion point is located and defines how it is displayed. This includes its size and shape, its blink rate (if any), etc. (Don't confuse this with java.awt.Cursor, which tracks the mouse, not the insertion point.)

Highlighter

Keeps track of which text should be highlighted and how that text is visually marked. Typically, this is done by painting a solid rectangle "behind" the text, but this is up to the implementation of this interface.

Keymap

Defines a hierarchy of Actions to be performed when certain keys are pressed. For example, pressing Ctrl-C may copy some text, or Command-V may paste at the current caret location. This is considered an L&F feature because different native L&Fs have different default keymaps.

As you might expect, Swing provides default implementations of these interfaces. Figure 21-1 shows these classes and interfaces and the relationships between them. Note that each Caret and Highlighter is associated with a single JTextComponent (set by its install( ) method) while Keymap has no direct relation to any JTextComponent, and therefore can be used by multiple components.

Figure 21-1. Caret, Highlighter, and Keymap class diagram
figs/swng2.2101.gif

In the next few sections, we'll take a closer look at these interfaces as well as the default implementations for Caret and Highlighter. The default implementation of Keymap is an inner class of JTextComponent, which we can't subclass directly. When we get to that discussion, we'll see why we typically won't need to.



Java Swing
Graphic Java 2: Mastering the Jfc, By Geary, 3Rd Edition, Volume 2: Swing
ISBN: 0130796670
EAN: 2147483647
Year: 2001
Pages: 289
Authors: David Geary

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net