java.lang.Objectjava.awt.Component
java.awt.Container
javax.swing.JComponent
javax.swing.text.JTextComponent
All Implemented Interfaces:
Scrollable, Accessible, HasGetTransferHandler, Serializable, MenuContainer, ImageObserver
Direct Known Subclasses:
NumberField, FieldTextField, NumberTextField, JEditorPane, ChecklistChooser, JTextField, FieldTextArea, MultiplexingTextField, JFormattedTextField, DefaultTextField, FrameEditorPane, JPasswordField, JTextArea, UpperCaseField, HelpContent, BorderlessTextField, JTextPane, JComboTree
JTextComponent is the base class for swing text
components. It tries to be compatible with the
java.awt.TextComponent class
where it can reasonably do so. Also provided are other services
for additional flexibility (beyond the pluggable UI and bean
support).
You can find information on how to use the functionality
this class provides in
General Rules for Using Text Components,
a section in The Java Tutorial.
CaretListener
interface that have been registered with the text component.
The UI will install a default caret unless a customized caret
has been set. DefaultCaret
tries to make itself visible which may lead to scrolling
of a text component within JScrollPane. The default caret
behavior can be changed by the DefaultCaret#setUpdatePolicy method.
Action interface,
using the TextAction implementation.
The set of commands supported by the text component can be
found with the #getActions method. These actions
can be bound to key events, fired from buttons, etc.
A javax.swing.text.Keymap lets an application bind key
strokes to actions.
In order to allow keymaps to be shared across multiple text components, they
can use actions that extend TextAction.
TextAction can determine which JTextComponent
most recently has or had focus and therefore is the subject of
the action (In the case that the ActionEvent
sent to the action doesn't contain the target text component as its source).
The input method framework
lets text components interact with input methods, separate software
components that preprocess events to let users enter thousands of
different characters using keyboards with far fewer keys.
JTextComponent is an active client of
the framework, so it implements the preferred user interface for interacting
with input methods. As a consequence, some key events do not reach the text
component because they are handled by an input method, and some text input
reaches the text component as committed text within an java.awt.event.InputMethodEvent instead of as a key event.
The complete text input is the combination of the characters in
keyTyped key events and committed text in input method events.
The AWT listener model lets applications attach event listeners to components in order to bind events to actions. Swing encourages the use of keymaps instead of listeners, but maintains compatibility with listeners by giving the listeners a chance to steal an event by consuming it.
Keyboard event and input method events are handled in the following stages, with each stage capable of consuming the event:
Stage |
KeyEvent |
InputMethodEvent |
|---|---|---|
| 1. | input methods | (generated here) |
| 2. | focus manager | |
| 3. | registered key listeners | registered input method listeners |
| 4. | input method handling in JTextComponent | |
| 5. | keymap handling using the current keymap | |
| 6. | keyboard handling in JComponent (e.g. accelerators, component navigation, etc.) | |
To maintain compatibility with applications that listen to key events but are not aware of input method events, the input method handling in stage 4 provides a compatibility mode for components that do not process input method events. For these components, the committed text is converted to keyTyped key events and processed in the key event pipeline starting at stage 3 instead of in the input method event pipeline.
By default the component will create a keymap (named DEFAULT_KEYMAP) that is shared by all JTextComponent instances as the default keymap. Typically a look-and-feel implementation will install a different keymap that resolves to the default keymap for those bindings not found in the different keymap. The minimal bindings include:

The model is defined by the Document interface. This is intended to provide a flexible text storage mechanism that tracks change during edits and can be extended to more sophisticated models. The model interfaces are meant to capture the capabilities of expression given by SGML, a system used to express a wide variety of content. Each modification to the document causes notification of the details of the change to be sent to all observers in the form of a DocumentEvent which allows the views to stay up to date with the model. This event is sent to observers that have implemented the DocumentListener interface and registered interest with the model being observed.
UndoableEdit records that can be used in conjunction
with a history buffer to provide the undo/redo support.
The support is provided by the Document model, which allows
one to attach UndoableEditListener implementations.
AbstractDocument
describes the assumptions of the protection provided.
The methods that are safe to call asynchronously are marked
with comments.
Warning:
Serialized objects of this class will not be compatible with
future Swing releases. The current serialization support is
appropriate for short term storage or RMI between applications running
the same version of Swing. As of 1.4, support for long term storage
of all JavaBeansTM
has been added to the java.beans package.
Please see java.beans.XMLEncoder .
attribute - : isContainer falseTimothy - PrinzingIgor - Kushnirskiy (printing support)| Nested Class Summary: | ||
|---|---|---|
| public static class | JTextComponent.KeyBinding | Binding record for creating key bindings.
Warning:
Serialized objects of this class will not be compatible with
future Swing releases. The current serialization support is
appropriate for short term storage or RMI between applications running
the same version of Swing. As of 1.4, support for long term storage
of all JavaBeansTM
has been added to the |
| public class | JTextComponent.AccessibleJTextComponent | This class implements accessibility support for the
JTextComponent class. It provides an implementation of
the Java Accessibility API appropriate to menu user-interface elements.
Warning:
Serialized objects of this class will not be compatible with
future Swing releases. The current serialization support is
appropriate for short term storage or RMI between applications running
the same version of Swing. As of 1.4, support for long term storage
of all JavaBeansTM
has been added to the |
| public static final class | JTextComponent.DropLocation | Represents a drop location for JTextComponents. |
| static class | JTextComponent.DefaultTransferHandler | A Simple TransferHandler that exports the data as a String, and imports the data from the String clipboard. This is only used if the UI hasn't supplied one, which would only happen if someone hasn't subclassed Basic. |
| static class | JTextComponent.DefaultKeymap | |
| static class | JTextComponent.KeymapWrapper | KeymapWrapper wraps a Keymap inside an InputMap. For KeymapWrapper
to be useful it must be used with a KeymapActionMap.
KeymapWrapper for the most part, is an InputMap with two parents.
The first parent visited is ALWAYS the Keymap, with the second
parent being the parent inherited from InputMap. If
keymap.getAction returns null, implying the Keymap
does not have a binding for the KeyStroke,
the parent is then visited. If the Keymap has a binding, the
Action is returned, if not and the KeyStroke represents a
KeyTyped event and the Keymap has a defaultAction,
DefaultActionKey is returned.
KeymapActionMap is then able to transate the object passed in to either message the Keymap, or message its default implementation. |
| static class | JTextComponent.KeymapActionMap | Wraps a Keymap inside an ActionMap. This is used with
a KeymapWrapper. If get is passed in
KeymapWrapper.DefaultActionKey, the default action is
returned, otherwise if the key is an Action, it is returned. |
| static class | JTextComponent.MutableCaretEvent | Event to use when firing a notification of change to caret position. This is mutable so that the event can be reused since caret events can be fairly high in bandwidth. |
| class | JTextComponent.InputMethodRequestsHandler | |
| class | JTextComponent.ComposedTextCaret | |
| Field Summary | ||
|---|---|---|
| public static final String | FOCUS_ACCELERATOR_KEY | The bound property name for the focus accelerator. |
| public static final String | DEFAULT_KEYMAP | The default keymap that will be shared by all
JTextComponent instances unless they
have had a different keymap set. |
| Fields inherited from java.awt.Container: |
|---|
| ncomponents, component, layoutMgr, containerListener, listeningChildren, listeningBoundsChildren, descendantsCount, INCLUDE_SELF, SEARCH_HEAVYWEIGHTS, modalComp, modalAppContext |
| Constructor: |
|---|
JTextComponent.
Listeners for caret events are established, and the pluggable
UI installed. The component is marked as editable. No layout manager
is used, because layout is managed by the view subsystem of text.
The document model is set to null. |
| Methods from java.lang.Object: |
|---|
| clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Method from javax.swing.text.JTextComponent Detail: |
|---|
|
|
|
|
null selections. |
null selections. |
Note: This method is meant to override
|
|
AccessibleContext associated with this
JTextComponent. For text components,
the AccessibleContext takes the form of an
AccessibleJTextComponent.
A new AccessibleJTextComponent instance
is created if necessary. |
|
|
|
|
|
|
|
|
This method is not meant for querying the drop location
from a {@code TransferHandler}, as the drop location is only
set after the {@code TransferHandler}'s When this property changes, a property change event with name "dropLocation" is fired by the component. |
|
|
|
|
|
|
null-named keymaps. |
|
NavigationFilter. NavigationFilter
is used by DefaultCaret and the default cursor movement
actions as a way to restrict the cursor movement. A null return value
implies the cursor movement and selection should not be restricted. |
|
The returned {@code Printable} shares the {@code document} with this {@code JTextComponent}. It is the responsibility of the developer to ensure that the {@code document} is not mutated while this {@code Printable} is used. Printing behavior is undefined when the {@code document} is mutated during printing. Page header and footer text can be added to the output by providing {@code MessageFormat} arguments. The printing code requests {@code Strings} from the formats, providing a single item which may be included in the formatted string: an {@code Integer} representing the current page number. The returned {@code Printable} when printed, formats the document content appropriately for the page size. For correct line wrapping the {@code imageable width} of all pages must be the same. See java.awt.print.PageFormat#getImageableWidth . This method is thread-safe, although most Swing methods are not. Please see How to Use Threads for more information. The returned {@code Printable} can be printed on any thread. This implementation returned {@code Printable} performs all painting on the Event Dispatch Thread, regardless of what thread it is used on. |
The default implementation of this is to simply return the visible area. Subclasses will likely be able to provide a much more reasonable value. |
Scrollable to match the height of the viewport.
For example a columnar text view that flowed text in left to
right columns could effectively disable vertical scrolling by
returning true here.
Scrolling containers, like |
Scrollable to match the width of the viewport.
For example a normal text view that supported line wrapping
would return true here, since it would be undesirable for
wrapped lines to disappear beyond the right
edge of the viewport. Note that returning true for a
Scrollable whose ancestor is a JScrollPane
effectively disables horizontal scrolling.
Scrolling containers, like |
The default implementation of this is to simply return 10% of the visible area. Subclasses are likely to be able to provide a much more reasonable value. |
TextComponent. If the selection is
null or the document empty, returns null. |
|
|
|
|
TextComponent.
If the underlying document is null,
will give a NullPointerException.
Note that text is not a bound property, so no PropertyChangeEvent
is fired when it changes. To listen for changes to the text,
use DocumentListener. |
|
event.
This will return one of:
JTextComponent does not register
itself with the ToolTipManager.
This means that tooltips will NOT be shown from the
TextUI unless registerComponent has
been invoked on the ToolTipManager. |
|
TextComponent is editable or not. |
Loads a keymap with a bunch of bindings. This can be used to take a static table of definitions and load them into some keymap. The following example illustrates an example of binding some keys to the cut, copy, and paste actions associated with a JTextComponent. A code fragment to accomplish this might look as follows:
The sets of bindings and actions may be empty but must be
non-null. |
|
setCaretPosition was
called. This forms a selection.
If the document is null, does nothing. The position
must be between 0 and the length of the component's text or else
an exception is thrown. |
JTextComponent.
This method is intended to be used only for debugging purposes, and the
content and format of the returned string may vary between
implementations. The returned string may be empty but may not
be null.
Overriding |
|
Note: In headless mode, no dialogs will be shown. This method calls the full featured MessageFormat, boolean, PrintService, PrintRequestAttributeSet, boolean) print method to perform printing. |
Note: In headless mode, no dialogs will be shown. This method calls the full featured MessageFormat, boolean, PrintService, PrintRequestAttributeSet, boolean) print method to perform printing. |
|