What are the purposes of using TextAction from AbstractAction? and how to use it for the following:
Caret
Selection in the JTextComponents
KeyBindings
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With