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 methodNote
Non-editable text components also have a caret though it may not be painted.
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. 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. 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.
modelToView
and viewToModel for determining this information.
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.
print methods are provided for basic
document printing. If more advanced printing is needed, use the
getPrintable method.
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 JavaBeans
has been added to the java.beans package.
Please see java..
Document, DocumentEvent, DocumentListener, Caret, CaretEvent, CaretListener, TextUI, View, ViewFactory
| Modifier and Type | Class and Description |
|---|---|
| public class | JTextComponent.
This class implements accessibility support for the
|
| pack-priv class | |
| pack-priv static class | |
| pack-priv static class | JTextComponent.
A Simple TransferHandler that exports the data as a String, and imports the data from the String clipboard. |
| private static class | |
| public static class | JTextComponent.
Represents a drop location for |
| pack-priv class | |
| public static class | JTextComponent.
Binding record for creating key bindings. |
| pack-priv static class | JTextComponent.
Wraps a Keymap inside an ActionMap. |
| pack-priv static class | JTextComponent.
KeymapWrapper wraps a Keymap inside an InputMap. |
| pack-priv static class | JTextComponent.
Event to use when firing a notification of change to caret position. |
| Modifier and Type | Field and Description |
|---|---|
| private transient Caret | caret
The caret used to display the insert position and navigate throughout the document. |
| private Color | |
| private transient JTextComponent. | |
| private boolean | checkedInputOverride
Set to true after the check for the override of processInputMethodEvent has been checked. |
| private SimpleAttributeSet | |
| private JTextComponent. | |
| private String | |
| private Position | |
| private Position | |
| public static final String | DEFAULT_KEYMAP
The default keymap that will be shared by all
|
| private static JTextComponent. | defaultTransferHandler
TransferHandler used if one hasn't been supplied by the UI. |
| private Color | |
| private boolean | |
| private transient JTextComponent. | dropLocation
The drop location. |
| private DropMode | dropMode
The drop mode for this component. |
| private boolean | |
| public static final String | FOCUS_ACCELERATOR_KEY
The bound property name for the focus accelerator. |
| private char | |
| private static JTextComponent | |
| private transient Highlighter | highlighter
The object responsible for managing highlights. |
| private transient InputMethodRequests | |
| private transient Keymap | keymap
The current key bindings in effect. |
| private static HashMap | |
| private Position | |
| private Position | |
| private Insets | |
| private static Cache | METHOD_OVERRIDDEN
Maps from class name to Boolean indicating if
|
| private Document | model
The document model. |
| private NavigationFilter | navigationFilter
Object responsible for restricting the cursor navigation. |
| private boolean | |
| private transient Caret | |
| private Color | |
| private Color |
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| public void | addCaretListener(CaretListener
the listener to be added listener)Adds a caret listener for notification of any changes to the caret. |
| public void | addInputMethodListener(InputMethodListener l)
Overrides java. Adds the specified input method listener to receive input method events from this component. |
| public static Keymap | Returns: the keymapthe name of the keymap (must be unique within the
collection of named keymaps in the document); the name may
be nm, Keymap null if the keymap is unnamed,
but the caller is responsible for managing the reference
returned as an unnamed keymap can't
be fetched by namethe parent keymap; this may be parent)null if
unspecified bindings need not be resolved in some other keymapAdds a new keymap into the keymap hierarchy. |
| private static void | |
| private static void | |
| pack-priv boolean | |
| public void | copy()
Transfers the currently selected range in the associated text model to the system clipboard, leaving the contents in the text model. |
| private void | |
| public void | cut()
Transfers the currently selected range in the associated text model to the system clipboard, removing the contents from the model. |
| pack-priv JTextComponent. | Returns: the drop location, ornullthe point to calculate a drop location for p)Hides javax. Calculates a drop location in this component, representing where a drop at the given point should insert data. |
| private void | |
| protected void | fireCaretUpdate(CaretEvent
the event e)Notifies all listeners that have registered interest for notification on this event type. |
| public AccessibleContext | Returns: anAccessibleJTextComponent that serves as the
AccessibleContext of this
JTextComponentOverrides java. Implements javax. Gets the |
| public Action[] | |
| public Caret | |
| public Color | |
| public CaretListener[] | Returns: all of this component'sCaretListeners
or an empty
array if no caret listeners are currently registeredReturns an array of all the caret listeners registered on this text component. |
| public int | Returns: the position of the text insertion caret for the text component ≥ 0Returns the position of the text insertion caret for the text component. |
| private int | |
| public Color | |
| public Document | |
| public boolean | Returns: the value of thedragEnabled propertyReturns whether or not automatic drag handling is enabled. |
| public final JTextComponent. | Returns: the drop locationReturns the location that this component should visually indicate
as the drop location during a DnD operation over the component,
or |
| public final DropMode | |
| public char | Returns: the keyReturns the key accelerator that will cause the receiving text component to get the focus. |
| pack-priv static final JTextComponent | |
| public Highlighter | |
| public InputMethodRequests | getInputMethodRequests()
Overrides java. Gets the input method request handler which supports requests from input methods for this component. |
| public Keymap | |
| public static Keymap | |
| private static synchronized HashMap | |
| public Insets | |
| public NavigationFilter | |
| public Dimension | Returns: thepreferredSize of a JViewport
whose view is this ScrollableImplements javax. Returns the preferred size of the viewport for a view component. |
| public Printable | Returns: aPrintable for use in printing content of this
JTextComponentthe text, in headerFormat, final MessageFormat MessageFormat, to be
used as the header, or null for no headerthe text, in footerFormat)MessageFormat, to be
used as the footer, or null for no footerReturns a |
| public int | Returns: the "block" increment for scrolling in the specified directionthe view area visible within the viewport visibleRect, int either orientation, int SwingConstants.VERTICAL or
SwingConstants.HORIZONTALless than zero to scroll up/left, greater than zero
for down/right direction)Implements javax. Components that display logical rows or columns should compute the scroll increment that will completely expose one block of rows or columns, depending on the value of orientation. |
| public boolean | Returns: true if a viewport should force the Scrollables height to match its ownImplements javax. Returns true if a viewport should always force the height of this
|
| public boolean | Returns: true if a viewport should force theScrollables
width to match its ownImplements javax. Returns true if a viewport should always force the width of this
|
| public int | Returns: the "unit" increment for scrolling in the specified directionthe view area visible within the viewport visibleRect, int either orientation, int SwingConstants.VERTICAL or
SwingConstants.HORIZONTALless than zero to scroll up/left, greater than
zero for down/right direction)Implements javax. Components that display logical rows or columns should compute the scroll increment that will completely expose one new row or column, depending on the value of orientation. |
| public String | |
| public Color | |
| public Color | |
| public int | |
| public int | |
| public String | Returns: the textthe offset ≥ 0 offs, int the length ≥ 0 len)Fetches a portion of the text represented by the component. |
| public String | |
| public String | Returns: the string to be used as the tooltip foreventthe event in question event)Overrides javax. Returns the string to be used as the tooltip for |
| public TextUI | Returns: the factoryOverrides javax. Fetches the user-interface factory for this text-oriented editor. |
| private void | installDefaultTransferHandlerIfNecessary()
If the current |
| private void | invokeAction(String name, Action altAction)
This is a convenience method that is only useful for
|
| public boolean | Returns: the boolean valueReturns the boolean indicating whether this
|
| public static void | loadKeymap(Keymap
the keymap map, JTextComponent.the bindings bindings,the set of actions actions)Loads a keymap with a bunch of bindings. |
| private void | |
| public Rectangle | Returns: the coordinates as a rectangle, with (r.x, r.y) as the location in the coordinate system, or null if the component does not yet have a positive size.the position ≥ 0 pos)
Deprecated
since 9. replaced by
modelToView2D(int)
Converts the given location in the model to a place in the view coordinate system. |
| public Rectangle2D | Returns: the coordinates as a rectangle, with (r.x, r.y) as the location in the coordinate system, or null if the component does not yet have a positive size.the position pos)>= 0Converts the given location in the model to a place in the view coordinate system. |
| public void | moveCaretPosition(int
the position pos)Moves the caret to a new position, leaving behind a mark
defined by the last time |
| protected String | Returns: a string representation of thisJTextComponentOverrides javax. Returns a string representation of this |
| public void | |
| public boolean | Returns: true, unless printing is canceled by the userA convenience print method that displays a print dialog, and then
prints this |
| public boolean | Returns: true, unless printing is canceled by the userthe text, in headerFormat, final MessageFormat MessageFormat, to be
used as the header, or null for no headerthe text, in footerFormat)MessageFormat, to be
used as the footer, or null for no footerA convenience print method that displays a print dialog, and then
prints this |
| public boolean | Returns: true, unless printing is canceled by the userthe text, in headerFormat, final MessageFormat MessageFormat, to be
used as the header, or null for no headerthe text, in footerFormat, final boolean MessageFormat, to be
used as the footer, or null for no footertrue to display a print dialog,
false otherwiseinitial service, final PrintRequestAttributeSet PrintService, or null for the
defaultthe job attributes to be applied to the print job, or
attributes, final boolean null for nonewhether to print in an interactive mode interactive)Prints the content of this |
| protected void | processInputMethodEvent(InputMethodEvent e)
Overrides java. Processes input method events occurring on this component by
dispatching them to any registered
|
| public void | read(Reader
the stream to read from in, Object an object describing the stream; this
might be a string, a File, a URL, etc. Some kinds
of documents (such as html for example) might be
able to make use of this information; if non- desc)null,
it is added as a property of the documentInitializes from a stream. |
| private void | readObject(ObjectInputStream s)
Hides javax. We use the |
| public void | |
| public static Keymap | Returns: the keymap that was removedthe name of the keymap to remove nm)Removes a named keymap previously added to the document. |
| public void | removeNotify()
Overrides javax. Notifies this component that it no longer has a parent component. |
| private void | |
| public void | replaceSelection(String
the content to replace the selection with content)Replaces the currently selected content with new content represented by the given string. |
| protected void | |
| protected boolean | Returns: true if the composed text exists and is saved,
false otherwisedocument position to identify the composed text location pos)Saves composed text around the specified position. |
| public void | select(int
the start position of the text selectionStart, int the end position of the text selectionEnd)Selects the text between the specified start and end positions. |
| public void | |
| public void | |
| public void | |
| public void | setCaretPosition(int
the position position)Sets the position of the text insertion caret for the
|
| public void | setComponentOrientation(ComponentOrientation o)
Overrides java. Sets the language-sensitive orientation that is to be used to order the elements or text within this component. |
| public void | |
| public void | |
| public void | setDragEnabled(boolean
whether or not to enable automatic drag handling b)Turns on or off automatic drag handling. |
| pack-priv Object | Returns: any saved state for this component, ornull if nonethe drop location (as calculated by
location,dropLocationForPoint) or null
if there's no longer a valid drop locationthe state object saved earlier for this component,
or state, boolean nullwhether or not the method is being called because an
actual drop occurred forDrop)Hides javax. Called to set or clear the drop location during a DnD operation. |
| public final void | |
| public void | setEditable(boolean
the boolean to be set b)Sets the specified boolean to indicate whether or not this
|
| public void | setFocusAccelerator(char
the key aKey)Sets the key accelerator that will cause the receiving text component to get the focus. |
| public void | |
| private void | |
| public void | |
| public void | |
| public void | |
| public void | |
| public void | |
| public void | setSelectionEnd(int
the end position of the text ≥ 0 selectionEnd)Sets the selection end to the specified position. |
| public void | setSelectionStart(int
the start position of the text ≥ 0 selectionStart)Sets the selection start to the specified position. |
| public void | |
| public void | |
| private boolean | |
| pack-priv void | updateInputMap(Keymap
the old oldKm, Keymap Keymapthe new newKm)KeymapUpdates the |
| public void | |
| public int | Returns: the offset ≥ 0 from the start of the document, or -1 if the component does not yet have a positive size.the location in the view to translate pt)
Deprecated
since 9. replaced by
viewToModel2D(Point2D)
Converts the given place in the view coordinate system to the nearest representative location in the model. |
| public int | Returns: the offset>= 0 from the start of the document,
or -1 if the component does not yet have a positive
size.the location in the view to translate pt)Converts the given place in the view coordinate system to the nearest representative location in the model. |
| public void |