Chapter 22. 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 actually implementing your own look-and-feel. Certain aspects of these components’ behavior and appearance can be modified directly using set() methods on JTextComponent. In addition, with the more powerful text components (JEditorPane, and anything that extends it, including JTextPane) you are able to 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. This will include an explanation of carets, highlighters, and keymaps. Chapter 23, will examine the various View classes.

JTextComponent UI Properties

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

Caret

Defines how the cursor is displayed. This includes the size and shape of the cursor, the blink rate (if any), etc.

Highlighter

Defines how selected text is highlighted. Typically this is done by painting a solid rectangle “behind” the text, but this is up to the implementation of this interface. Highlighter also defines two inner-interfaces that we’ll look at.

Keymap

Defines the Actions performed when certain keys are pressed. For example, pressing CTRL-C may copy some text and CTRL-V may ...

Get Java Swing 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.