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 ...

Get Java Swing, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.