JavaTM 2 Platform
Std. Ed. v1.3.1

java.awt
Class TextComponent

java.lang.Object
  |
  +--java.awt.Component
        |
        +--java.awt.TextComponent
All Implemented Interfaces:
Accessible, ImageObserver, MenuContainer, Serializable
Direct Known Subclasses:
TextArea, TextField

public class TextComponent
extends Component
implements Accessible

The 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.

Since:
JDK1.0
See Also:
Serialized Form

Inner Class Summary
protected  class TextComponent.AccessibleAWTTextComponent
          This class implements accessibility support for the TextComponent class.
 
Inner classes inherited from class java.awt.Component
Component.AccessibleAWTComponent
 
Field Summary
protected  TextListener textListener
           
 
Fields inherited from class java.awt.Component
BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
 
Fields inherited from interface java.awt.image.ImageObserver
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
 
Method Summary
 void addNotify()
          Makes this Component displayable by connecting it to a native screen resource.
 void addTextListener(TextListener l)
          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 component.
 AccessibleContext getAccessibleContext()
          Gets the AccessibleContext associated with this TextComponent.
 Color getBackground()
          Gets the background color of this text component.
 int getCaretPosition()
          Gets the position of the text insertion caret for this text component.
 EventListener[] getListeners(Class listenerType)
          Return an array of all the listeners that were added to the TextComponent with addXXXListener(), where XXX is the name of the listenerType argument.
 String getSelectedText()
          Gets the selected text from the text that is presented by this text component.
 int getSelectionEnd()
          Gets the end position of the selected text in this text component.
 int getSelectionStart()
          Gets the start position of the selected text in this text component.
 String getText()
          Gets the text that is presented by this text component.
 boolean isEditable()
          Indicates whether or not this text component is editable.
protected  String paramString()
          Returns the parameter string representing the state of this text component.
protected  void processEvent(AWTEvent e)
          Processes events on this text component.
protected  void processTextEvent(TextEvent e)
          Processes text events occurring on this text component by dispatching them to any registered TextListener objects.
 void removeNotify()
          Removes the TextComponent's peer.
 void removeTextListener(TextListener l)
          Removes the specified text event listener so that it no longer receives text events from this text component If l is null, 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 selectAll()
          Selects all the text in this text component.
 void setBackground(Color c)
          Sets the background color of this text component.
 void setCaretPosition(int position)
          Sets the position of the text insertion caret for this text component.
 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 setText(String t)
          Sets the text that is presented by this text component to be the specified text.
 
Methods inherited from class java.awt.Component
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addPropertyChangeListener, addPropertyChangeListener, bounds, checkImage, checkImage, coalesceEvents, contains, contains, createImage, createImage, deliverEvent, disable, disableEvents, dispatchEvent, doLayout, enable, enable, enableEvents, firePropertyChange, getAlignmentX, getAlignmentY, getBounds, getBounds, getColorModel, getComponentAt, getComponentAt, getComponentOrientation, getCursor, getDropTarget, getFont, getFontMetrics, getForeground, getGraphics, getGraphicsConfiguration, getHeight, getInputContext, getInputMethodRequests, getLocale, getLocation, getLocation, getLocationOnScreen, getMaximumSize, getMinimumSize, getName, getParent, getPeer, getPreferredSize, getSize, getSize, getToolkit, getTreeLock, getWidth, getX, getY, gotFocus, handleEvent, hasFocus, hide, imageUpdate, inside, invalidate, isDisplayable, isDoubleBuffered, isEnabled, isFocusTraversable, isLightweight, isOpaque, 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, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, repaint, requestFocus, reshape, resize, resize, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setEnabled, setFont, setForeground, setLocale, setLocation, setLocation, setName, setSize, setSize, setVisible, show, show, size, toString, transferFocus, update, validate
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

textListener

protected transient TextListener textListener
Method Detail

enableInputMethods

public void enableInputMethods(boolean enable)
Description copied from class: Component
Enables or disables input method support for this component. If input method support is enabled and the 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. By default, input method support is enabled.
Overrides:
enableInputMethods in class Component
Following copied from class: java.awt.Component
Parameters:
enable - true to enable, false to disable.
See Also:
Component.processKeyEvent(java.awt.event.KeyEvent)

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.
Overrides:
addNotify in class Component
See Also:
removeNotify()

removeNotify

public void removeNotify()
Removes the TextComponent's peer. The peer allows us to modify the appearance of the TextComponent without changing its functionality.
Overrides:
removeNotify in class Component
Following copied from class: java.awt.Component
See Also:
Component.isDisplayable(), Component.addNotify()

setText

public void setText(String t)
Sets the text that is presented by this text component to be the specified text.
Parameters:
t - the new text. If this parameter is null then the text is set to the empty string "".
See Also:
getText()

getText

public String getText()
Gets the text that is presented by this text component.
See Also:
setText(java.lang.String)

getSelectedText

public String getSelectedText()
Gets the selected text from the text that is presented by this text component.
Returns:
the selected text of this text component.
See Also:
select(int, int)

isEditable

public boolean isEditable()
Indicates whether or not this text component is editable.
Returns:
true if this text component is editable; false otherwise.
Since:
JDK1.0
See Also:
setEditable(boolean)

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 to false, 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:
JDK1.0
See Also:
isEditable()

getBackground

public Color 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 class Component
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:
JDK1.0
See Also:
setBackground

setBackground

public void setBackground(Color c)
Sets the background color of this text component.
Overrides:
setBackground in class Component
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:
JDK1.0
See Also:
getBackground()

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(int), getSelectionEnd()

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 for selectionStart that is out of bounds, the method enforces these constraints silently, and without failure.
Parameters:
selectionStart - the start position of the selected text.
Since:
JDK1.1
See Also:
getSelectionStart(), setSelectionEnd(int)

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(int), getSelectionStart()

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 for selectionEnd that is out of bounds, the method enforces these constraints silently, and without failure.
Parameters:
selectionEnd - the end position of the selected text.
Since:
JDK1.1
See Also:
getSelectionEnd(), setSelectionStart(int)

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 at endPosition 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 to be selected.
selectionEnd - the zero-based end position of the text to be selected. The character at selectionEnd is not selected.
See Also:
setSelectionStart(int), setSelectionEnd(int), selectAll()

selectAll

public void selectAll()
Selects all the text in this text component.
See Also:
select(int, int)

setCaretPosition

public void setCaretPosition(int position)
Sets the position of the text insertion caret for this text component. The caret position is constrained to be at or before the current selection end. If the caller supplies a value for position that is greater than the end of the component's text, the caret position is set to the end of the component's text. This happens silently and without failure. The caret position also cannot be set to less than zero, the beginning of the component's text. If the caller supplies a value for position that is less than zero, an IllegalArgumentException is thrown.
Parameters:
position - the position of the text insertion caret.
Throws:
IllegalArgumentException - if the value supplied for position is less than zero.
Since:
JDK1.1

getCaretPosition

public int getCaretPosition()
Gets the position of the text insertion caret for this text component.
Returns:
the position of the text insertion caret.
Since:
JDK1.1

addTextListener

public void addTextListener(TextListener l)
Adds the specified text event listener to receive text events from this text component. If l is null, no exception is thrown and no action is performed.
Parameters:
l - the text event listener

removeTextListener

public void removeTextListener(TextListener l)
Removes the specified text event listener so that it no longer receives text events from this text component If l is null, no exception is thrown and no action is performed.
Parameters:
l - the text listener.
Since:
JDK1.1
See Also:
TextListener, addTextListener(java.awt.event.TextListener)

getListeners

public EventListener[] getListeners(Class listenerType)
Return an array of all the listeners that were added to the TextComponent with addXXXListener(), where XXX is the name of the listenerType argument. For example, to get all of the TextListeners for the given TextComponent t, one would write:
 TextListener[] tls = (TextListener[])(t.getListeners(TextListener.class))
 
If no such listener list exists, then an empty array is returned.
Overrides:
getListeners in class Component
Parameters:
listenerType - Type of listeners requested
Returns:
all of the listeners of the specified type supported by this text component
Since:
1.3

processEvent

protected void processEvent(AWTEvent e)
Processes events on this text component. If the event is a TextEvent, it invokes the processTextEvent method, else it invokes its superclass's processEvent.
Overrides:
processEvent in class Component
Parameters:
e - the event

processTextEvent

protected void processTextEvent(TextEvent e)
Processes text events occurring on this text component by dispatching them to any registered TextListener 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) A TextListener object is registered via addTextListener() b) Text events are enabled via enableEvents()
Parameters:
e - the text event
See Also:
Component.enableEvents(long)

paramString

protected String paramString()
Returns the parameter string representing the state of this text component. This string is useful for debugging.
Overrides:
paramString in class Component
Returns:
the parameter string of this text component.

getAccessibleContext

public AccessibleContext 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 interface Accessible
Overrides:
getAccessibleContext in class Component
Returns:
an AccessibleAWTTextComponent that serves as the AccessibleContext of this TextComponent

JavaTM 2 Platform
Std. Ed. v1.3.1

Submit a bug or feature
For further API reference and developer documentation, see Java 2 SDK SE Developer Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.

Java, Java 2D, and JDBC are trademarks or registered trademarks of Oracle and/or its affiliates, in the US and other countries.
Copyright © 1995, 2010 Oracle and/or its affiliates. All rights reserved.