How to use TextAction

为君一笑 提交于 2019-11-27 05:36:26
trashgod

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.

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!