java.awt
Class TextComponent

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

public class TextComponent
extends Component
implements Serializable, Accessible

This class provides common functionality for widgets than contain text.

See Also:
Serialized Form

Nested Class Summary
protected  class TextComponent.AccessibleAWTTextComponent
           
 
Nested classes/interfaces inherited from class java.awt.Component
Component.AccessibleAWTComponent, Component.BltBufferStrategy, Component.FlipBufferStrategy
 
Field Summary
protected  TextListener textListener
          A list of listeners that will receive events from this object.
 
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 addTextListener(TextListener listener)
          Adds a new listener to the list of text listeners for this component.
 AccessibleContext getAccessibleContext()
          Gets the AccessibleContext associated with this TextComponent.
 int getCaretPosition()
          Returns the current caret position in the text.
<T extends EventListener>
T[]
getListeners(Class<T> listenerType)
          Returns an array of all the objects currently registered as FooListeners upon this TextComponent.
 String getSelectedText()
          Returns a string that contains the text that is currently selected.
 int getSelectionEnd()
          Returns the ending position of the selected text region.
 int getSelectionStart()
          Returns the starting position of the selected text region.
 String getText()
          Returns the text in this component
 TextListener[] getTextListeners()
          Returns all text listeners registered to this object.
 boolean isEditable()
          Tests whether or not this component's text can be edited.
protected  String paramString()
          Returns a debugging string.
protected  void processEvent(AWTEvent event)
          Processes the specified event for this component.
protected  void processTextEvent(TextEvent event)
          Processes the specified text event by dispatching it to any listeners that are registered.
 void removeNotify()
          Notifies the component that it should destroy its native peer.
 void removeTextListener(TextListener listener)
          Removes the specified listener from the list of listeners for this component.
 void select(int selectionStart, int selectionEnd)
          This method sets the selected text range to the text between the specified start and end positions.
 void selectAll()
          Selects all of the text in the component.
 void setCaretPosition(int caretPosition)
          Sets the caret position to the specified value.
 void setEditable(boolean editable)
          Sets whether or not this component's text can be edited.
 void setSelectionEnd(int selectionEnd)
          Sets the ending position of the selected region to the specified value.
 void setSelectionStart(int selectionStart)
          Sets the starting position of the selected region to the specified value.
 void setText(String text)
          Sets the text in this component to the specified string.
 
Methods inherited from class java.awt.Component
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, addNotify, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, bounds, checkImage, checkImage, coalesceEvents, contains, contains, createImage, createImage, createVolatileImage, createVolatileImage, deliverEvent, disable, disableEvents, dispatchEvent, doLayout, enable, enable, enableEvents, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getAlignmentX, getAlignmentY, getBackground, 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, getPeer, 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, requestFocusInWindow, requestFocusInWindow, reshape, resize, resize, setBackground, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setEnabled, setFocusable, setFocusTraversalKeys, setFocusTraversalKeysEnabled, setFont, setForeground, setIgnoreRepaint, setLocale, setLocation, setLocation, setMaximumSize, setMinimumSize, setName, setPreferredSize, setSize, setSize, setVisible, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycle, 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
A list of listeners that will receive events from this object.

Method Detail

getText

public String getText()
Returns the text in this component

Returns:
The text in this component.

setText

public void setText(String text)
Sets the text in this component to the specified string.

Parameters:
text - The new text for this component.

getSelectedText

public String getSelectedText()
Returns a string that contains the text that is currently selected.

Returns:
The currently selected text region.

getSelectionStart

public int getSelectionStart()
Returns the starting position of the selected text region. If the text is not selected then caret position is returned.

Returns:
The starting position of the selected text region.

setSelectionStart

public void setSelectionStart(int selectionStart)
Sets the starting position of the selected region to the specified value. If the specified value is out of range, then it will be silently changed to the nearest legal value.

Parameters:
selectionStart - The new start position for selected text.

getSelectionEnd

public int getSelectionEnd()
Returns the ending position of the selected text region. If the text is not selected, then caret position is returned

Returns:
The ending position of the selected text region.

setSelectionEnd

public void setSelectionEnd(int selectionEnd)
Sets the ending position of the selected region to the specified value. If the specified value is out of range, then it will be silently changed to the nearest legal value.

Parameters:
selectionEnd - The new start position for selected text.

select

public void select(int selectionStart,
                   int selectionEnd)
This method sets the selected text range to the text between the specified start and end positions. Illegal values for these positions are silently fixed.

Parameters:
selectionStart - The new start position for the selected text.
selectionEnd - The new end position for the selected text.

selectAll

public void selectAll()
Selects all of the text in the component.


getCaretPosition

public int getCaretPosition()
Returns the current caret position in the text.

Returns:
The caret position in the text.

setCaretPosition

public void setCaretPosition(int caretPosition)
Sets the caret position to the specified value.

Parameters:
caretPosition - The new caret position.
Throws:
IllegalArgumentException - If the value supplied for position is less than zero.
Since:
1.1

isEditable

public boolean isEditable()
Tests whether or not this component's text can be edited.

Returns:
true if the text can be edited, false otherwise.

setEditable

public void setEditable(boolean editable)
Sets whether or not this component's text can be edited.

Parameters:
editable - true to enable editing of the text, false to disable it.

removeNotify

public void removeNotify()
Notifies the component that it should destroy its native peer.

Overrides:
removeNotify in class Component
See Also:
Component.isDisplayable(), Component.addNotify()

addTextListener

public void addTextListener(TextListener listener)
Adds a new listener to the list of text listeners for this component.

Parameters:
listener - The listener to be added.

removeTextListener

public void removeTextListener(TextListener listener)
Removes the specified listener from the list of listeners for this component.

Parameters:
listener - The listener to remove.

processEvent

protected void processEvent(AWTEvent event)
Processes the specified event for this component. Text events are processed by calling the processTextEvent() method. All other events are passed to the superclass method.

Overrides:
processEvent in class Component
Parameters:
event - The event to process.
See Also:
Component.processComponentEvent(ComponentEvent), Component.processFocusEvent(FocusEvent), Component.processKeyEvent(KeyEvent), Component.processMouseEvent(MouseEvent), Component.processMouseMotionEvent(MouseEvent), Component.processInputMethodEvent(InputMethodEvent), Component.processHierarchyEvent(HierarchyEvent), Component.processMouseWheelEvent(MouseWheelEvent)

processTextEvent

protected void processTextEvent(TextEvent event)
Processes the specified text event by dispatching it to any listeners that are registered. Note that this method will only be called if text event's are enabled. This will be true if there are any registered listeners, or if the event has been specifically enabled using enableEvents().

Parameters:
event - The text event to process.

paramString

protected String paramString()
Returns a debugging string.

Overrides:
paramString in class Component
Returns:
A debugging string.

getListeners

public <T extends EventListener> T[] getListeners(Class<T> listenerType)
Returns an array of all the objects currently registered as FooListeners upon this TextComponent. FooListeners are registered using the addFooListener method.

Overrides:
getListeners in class Component
Parameters:
listenerType - the class of listeners to filter (null not permitted).
Returns:
An array of registered listeners.
Throws:
ClassCastException - If listenerType doesn't specify a class or interface that implements java.util.EventListener.
See Also:
Component.getComponentListeners(), Component.getFocusListeners(), Component.getHierarchyListeners(), Component.getHierarchyBoundsListeners(), Component.getKeyListeners(), Component.getMouseListeners(), Component.getMouseMotionListeners(), Component.getMouseWheelListeners(), Component.getInputMethodListeners(), Component.getPropertyChangeListeners()

getTextListeners

public TextListener[] getTextListeners()
Returns all text listeners registered to this object.


getAccessibleContext

public AccessibleContext getAccessibleContext()
Gets the AccessibleContext associated with this TextComponent. The context is created, if necessary.

Specified by:
getAccessibleContext in interface Accessible
Overrides:
getAccessibleContext in class Component
Returns:
the associated context