Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use TextAction

What are the purposes of using TextAction from AbstractAction? and how to use it for the following:

  • Caret

  • Selection in the JTextComponents

  • KeyBindings

like image 621
mKorbel Avatar asked Apr 09 '12 15:04

mKorbel


2 Answers

From Java Swing 2nd Edition:

All text components share a set of default Actions. Each of these Actions are instances of TextAction by default. JTextComponent provides a private static EditorKit which consists of a set of four pre-built TextActions shared by all text components through the use of a default Keymap instance.

JTextComponent maintains a private reference to the text component that most recently had the keyboard focus. TextActions are designed to take advantage of this, and each TextAction will operate on this component when it’s invoked in the event that the source of the invoking event is not a text component.

Here are some examples that implement TextAction:

Custom Editing Command

Finding Word Boundaries

like image 104
tenorsax Avatar answered Sep 30 '22 04:09

tenorsax


While composing this answer, I recalled a venerable HTMLDocumentEditor by Charles Bell that illustrates the typical usage of the subclasses found in javax.swing.text.TextAction. That editor is listed among the credits of Metaphase Editor. This related example showing actions found in StyledEditorKit follows the same approach. All such actions are suitable for Key Bindings, and all operate on the current selection maintained by the Caret, whenever possible.

like image 21
trashgod Avatar answered Sep 30 '22 06:09

trashgod