O'Reilly logo

Java Swing by Dave Wood, Marc Loy, Robert Eckstein

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required