- All Implemented Interfaces:
ImageObserver
,MenuContainer
,Serializable
,Accessible
TextComponent
class is the superclass of
any component that allows the editing of some text.
A text component embodies a string of text. The
TextComponent
class defines a set of methods
that determine whether or not this text is editable. If the
component is editable, it defines another set of methods
that supports a text insertion caret.
In addition, the class defines methods that are used to maintain a current selection from the text. The text selection, a substring of the component's text, is the target of editing operations. It is also referred to as the selected text.
-
Nested Class Summary
Modifier and TypeClassDescriptionprotected class
This class implements accessibility support for theTextComponent
class.Nested classes/interfaces declared in class java.awt.Component
Component.AccessibleAWTComponent, Component.BaselineResizeBehavior, Component.BltBufferStrategy, Component.FlipBufferStrategy
-
Field Summary
Modifier and TypeFieldDescriptionprotected TextListener
A list of listeners that will receive events from this object.Fields declared in class java.awt.Component
accessibleContext, BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
Fields declared in interface java.awt.image.ImageObserver
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
-
Method Summary
Modifier and TypeMethodDescriptionvoid
Makes this Component displayable by connecting it to a native screen resource.void
Adds the specified text event listener to receive text events from this text component.void
enableInputMethods
(boolean enable) Enables or disables input method support for this text component.Gets the AccessibleContext associated with this TextComponent.Gets the background color of this text component.int
Returns the position of the text insertion caret.<T extends EventListener>
T[]getListeners
(Class<T> listenerType) Returns an array of all the objects currently registered asFooListener
s upon thisTextComponent
.Returns the selected text from the text that is presented by this text component.int
Gets the end position of the selected text in this text component.int
Gets the start position of the selected text in this text component.getText()
Returns the text that is presented by this text component.Returns an array of all the text listeners registered on this text component.boolean
Indicates whether or not this text component is editable.protected String
Returns a string representing the state of thisTextComponent
.protected void
Processes events on this text component.protected void
Processes text events occurring on this text component by dispatching them to any registeredTextListener
objects.void
Removes theTextComponent
's peer.void
Removes the specified text event listener so that it no longer receives text events from this text component Ifl
isnull
, no exception is thrown and no action is performed.void
select
(int selectionStart, int selectionEnd) Selects the text between the specified start and end positions.void
Selects all the text in this text component.void
Sets the background color of this text component.void
setCaretPosition
(int position) Sets the position of the text insertion caret.void
setEditable
(boolean b) Sets the flag that determines whether or not this text component is editable.void
setSelectionEnd
(int selectionEnd) Sets the selection end for this text component to the specified position.void
setSelectionStart
(int selectionStart) Sets the selection start for this text component to the specified position.void
Sets the text that is presented by this text component to be the specified text.Methods declared in class java.awt.Component
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, bounds, checkImage, checkImage, coalesceEvents, contains, contains, createImage, createImage, createVolatileImage, createVolatileImage, deliverEvent, disable, disableEvents, dispatchEvent, doLayout, enable, enable, enableEvents, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getAlignmentX, getAlignmentY, getBaseline, getBaselineResizeBehavior, getBounds, getBounds, getColorModel, getComponentAt, getComponentAt, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeys, getFocusTraversalKeysEnabled, getFont, getFontMetrics, getForeground, getGraphics, getGraphicsConfiguration, getHeight, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocale, getLocation, getLocation, getLocationOnScreen, getMaximumSize, getMinimumSize, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPreferredSize, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getSize, getToolkit, getTreeLock, getWidth, getX, getY, gotFocus, handleEvent, hasFocus, hide, imageUpdate, inside, invalidate, isBackgroundSet, isCursorSet, isDisplayable, isDoubleBuffered, isEnabled, isFocusable, isFocusCycleRoot, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isOpaque, isPreferredSizeSet, isShowing, isValid, isVisible, keyDown, keyUp, layout, list, list, list, list, list, locate, location, lostFocus, minimumSize, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paint, paintAll, postEvent, preferredSize, prepareImage, prepareImage, print, printAll, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processKeyEvent, processMouseEvent, processMouseMotionEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, repaint, requestFocus, requestFocus, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, requestFocusInWindow, reshape, resize, resize, revalidate, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setEnabled, setFocusable, setFocusTraversalKeys, setFocusTraversalKeysEnabled, setFont, setForeground, setIgnoreRepaint, setLocale, setLocation, setLocation, setMaximumSize, setMinimumSize, setMixingCutoutShape, setName, setPreferredSize, setSize, setSize, setVisible, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycle, update, validate
-
Field Details
-
textListener
A list of listeners that will receive events from this object.
-
-
Method Details
-
enableInputMethods
public void enableInputMethods(boolean enable) Enables or disables input method support for this text component. If input method support is enabled and the text component also processes key events, incoming events are offered to the current input method and will only be processed by the component or dispatched to its listeners if the input method does not consume them. Whether and how input method support for this text component is enabled or disabled by default is implementation dependent.- Overrides:
enableInputMethods
in classComponent
- Parameters:
enable
- true to enable, false to disable- Since:
- 1.2
- See Also:
-
addNotify
public void addNotify()Makes this Component displayable by connecting it to a native screen resource. This method is called internally by the toolkit and should not be called directly by programs. -
removeNotify
public void removeNotify()Removes theTextComponent
's peer. The peer allows us to modify the appearance of theTextComponent
without changing its functionality.- Overrides:
removeNotify
in classComponent
- See Also:
-
setText
Sets the text that is presented by this text component to be the specified text.- Parameters:
t
- the new text; if this parameter isnull
then the text is set to the empty string ""- See Also:
-
getText
Returns the text that is presented by this text component. By default, this is an empty string.- Returns:
- the value of this
TextComponent
- See Also:
-
getSelectedText
Returns the selected text from the text that is presented by this text component.- Returns:
- the selected text of this text component
- See Also:
-
isEditable
public boolean isEditable()Indicates whether or not this text component is editable.- Returns:
true
if this text component is editable;false
otherwise.- Since:
- 1.0
- See Also:
-
setEditable
public void setEditable(boolean b) Sets the flag that determines whether or not this text component is editable.If the flag is set to
true
, this text component becomes user editable. If the flag is set tofalse
, the user cannot change the text of this text component. By default, non-editable text components have a background color of SystemColor.control. This default can be overridden by calling setBackground.- Parameters:
b
- a flag indicating whether this text component is user editable.- Since:
- 1.0
- See Also:
-
getBackground
Gets the background color of this text component. By default, non-editable text components have a background color of SystemColor.control. This default can be overridden by calling setBackground.- Overrides:
getBackground
in classComponent
- Returns:
- This text component's background color. If this text component does not have a background color, the background color of its parent is returned.
- Since:
- 1.0
- See Also:
-
setBackground
Sets the background color of this text component.- Overrides:
setBackground
in classComponent
- Parameters:
c
- The color to become this text component's color. If this parameter is null then this text component will inherit the background color of its parent.- Since:
- 1.0
- See Also:
-
getSelectionStart
public int getSelectionStart()Gets the start position of the selected text in this text component.- Returns:
- the start position of the selected text
- See Also:
-
setSelectionStart
public void setSelectionStart(int selectionStart) Sets the selection start for this text component to the specified position. The new start point is constrained to be at or before the current selection end. It also cannot be set to less than zero, the beginning of the component's text. If the caller supplies a value forselectionStart
that is out of bounds, the method enforces these constraints silently, and without failure.- Parameters:
selectionStart
- the start position of the selected text- Since:
- 1.1
- See Also:
-
getSelectionEnd
public int getSelectionEnd()Gets the end position of the selected text in this text component.- Returns:
- the end position of the selected text
- See Also:
-
setSelectionEnd
public void setSelectionEnd(int selectionEnd) Sets the selection end for this text component to the specified position. The new end point is constrained to be at or after the current selection start. It also cannot be set beyond the end of the component's text. If the caller supplies a value forselectionEnd
that is out of bounds, the method enforces these constraints silently, and without failure.- Parameters:
selectionEnd
- the end position of the selected text- Since:
- 1.1
- See Also:
-
select
public void select(int selectionStart, int selectionEnd) Selects the text between the specified start and end positions.This method sets the start and end positions of the selected text, enforcing the restriction that the start position must be greater than or equal to zero. The end position must be greater than or equal to the start position, and less than or equal to the length of the text component's text. The character positions are indexed starting with zero. The length of the selection is
endPosition
-startPosition
, so the character atendPosition
is not selected. If the start and end positions of the selected text are equal, all text is deselected.If the caller supplies values that are inconsistent or out of bounds, the method enforces these constraints silently, and without failure. Specifically, if the start position or end position is greater than the length of the text, it is reset to equal the text length. If the start position is less than zero, it is reset to zero, and if the end position is less than the start position, it is reset to the start position.
- Parameters:
selectionStart
- the zero-based index of the first character (char
value) to be selectedselectionEnd
- the zero-based end position of the text to be selected; the character (char
value) atselectionEnd
is not selected- See Also:
-
selectAll
public void selectAll()Selects all the text in this text component.- See Also:
-
setCaretPosition
public void setCaretPosition(int position) Sets the position of the text insertion caret. The caret position is constrained to be between 0 and the last character of the text, inclusive. If the passed-in value is greater than this range, the value is set to the last character (or 0 if theTextComponent
contains no text) and no error is returned. If the passed-in value is less than 0, anIllegalArgumentException
is thrown.- Parameters:
position
- the position of the text insertion caret- Throws:
IllegalArgumentException
- ifposition
is less than zero- Since:
- 1.1
-
getCaretPosition
public int getCaretPosition()Returns the position of the text insertion caret. The caret position is constrained to be between 0 and the last character of the text, inclusive. If the text or caret have not been set, the default caret position is 0.- Returns:
- the position of the text insertion caret
- Since:
- 1.1
- See Also:
-
addTextListener
Adds the specified text event listener to receive text events from this text component. Ifl
isnull
, no exception is thrown and no action is performed.Refer to AWT Threading Issues for details on AWT's threading model.
- Parameters:
l
- the text event listener- See Also:
-
removeTextListener
Removes the specified text event listener so that it no longer receives text events from this text component Ifl
isnull
, no exception is thrown and no action is performed.Refer to AWT Threading Issues for details on AWT's threading model.
- Parameters:
l
- the text listener- Since:
- 1.1
- See Also:
-
getTextListeners
Returns an array of all the text listeners registered on this text component.- Returns:
- all of this text component's
TextListener
s or an empty array if no text listeners are currently registered - Since:
- 1.4
- See Also:
-
getListeners
Returns an array of all the objects currently registered asFooListener
s upon thisTextComponent
.FooListener
s are registered using theaddFooListener
method.You can specify the
listenerType
argument with a class literal, such asFooListener.class
. For example, you can query aTextComponent t
for its text listeners with the following code:TextListener[] tls = (TextListener[])(t.getListeners(TextListener.class));
If no such listeners exist, this method returns an empty array.- Overrides:
getListeners
in classComponent
- Type Parameters:
T
- the type of the listeners- Parameters:
listenerType
- the type of listeners requested; this parameter should specify an interface that descends fromjava.util.EventListener
- Returns:
- an array of all objects registered as
FooListener
s on this text component, or an empty array if no such listeners have been added - Throws:
ClassCastException
- iflistenerType
doesn't specify a class or interface that implementsjava.util.EventListener
- Since:
- 1.3
- See Also:
-
processEvent
Processes events on this text component. If the event is aTextEvent
, it invokes theprocessTextEvent
method else it invokes its superclass'sprocessEvent
.Note that if the event parameter is
null
the behavior is unspecified and may result in an exception.- Overrides:
processEvent
in classComponent
- Parameters:
e
- the event- See Also:
-
processTextEvent
Processes text events occurring on this text component by dispatching them to any registeredTextListener
objects.NOTE: This method will not be called unless text events are enabled for this component. This happens when one of the following occurs:
- A
TextListener
object is registered viaaddTextListener
- Text events are enabled via
enableEvents
Note that if the event parameter is
null
the behavior is unspecified and may result in an exception.- Parameters:
e
- the text event- See Also:
- A
-
paramString
Returns a string representing the state of thisTextComponent
. 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 benull
.- Overrides:
paramString
in classComponent
- Returns:
- the parameter string of this text component
-
getAccessibleContext
Gets the AccessibleContext associated with this TextComponent. For text components, the AccessibleContext takes the form of an AccessibleAWTTextComponent. A new AccessibleAWTTextComponent instance is created if necessary.- Specified by:
getAccessibleContext
in interfaceAccessible
- Overrides:
getAccessibleContext
in classComponent
- Returns:
- an AccessibleAWTTextComponent that serves as the AccessibleContext of this TextComponent
- Since:
- 1.3
-