java.awt

Class TextComponent

public class TextComponent extends Component implements Serializable, Accessible

This class provides common functionality for widgets than contain text.
Nested Class Summary
protected classTextComponent.AccessibleAWTTextComponent
Field Summary
protected TextListenertextListener
A list of listeners that will receive events from this object.
Method Summary
voidaddTextListener(TextListener listener)
Adds a new listener to the list of text listeners for this component.
AccessibleContextgetAccessibleContext()
Gets the AccessibleContext associated with this TextComponent.
intgetCaretPosition()
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.
StringgetSelectedText()
Returns a string that contains the text that is currently selected.
intgetSelectionEnd()
Returns the ending position of the selected text region.
intgetSelectionStart()
Returns the starting position of the selected text region.
StringgetText()
Returns the text in this component
TextListener[]getTextListeners()
Returns all text listeners registered to this object.
booleanisEditable()
Tests whether or not this component's text can be edited.
protected StringparamString()
Returns a debugging string.
protected voidprocessEvent(AWTEvent event)
Processes the specified event for this component.
protected voidprocessTextEvent(TextEvent event)
Processes the specified text event by dispatching it to any listeners that are registered.
voidremoveNotify()
Notifies the component that it should destroy its native peer.
voidremoveTextListener(TextListener listener)
Removes the specified listener from the list of listeners for this component.
voidselect(int selectionStart, int selectionEnd)
This method sets the selected text range to the text between the specified start and end positions.
voidselectAll()
Selects all of the text in the component.
voidsetCaretPosition(int caretPosition)
Sets the caret position to the specified value.
voidsetEditable(boolean editable)
Sets whether or not this component's text can be edited.
voidsetSelectionEnd(int selectionEnd)
Sets the ending position of the selected region to the specified value.
voidsetSelectionStart(int selectionStart)
Sets the starting position of the selected region to the specified value.
voidsetText(String text)
Sets the text in this component to the specified string.

Field Detail

textListener

protected transient TextListener textListener
A list of listeners that will receive events from this object.

Method Detail

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.

getAccessibleContext

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

Returns: the associated context

getCaretPosition

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

Returns: The caret position in the text.

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.

Throws: ClassCastException If listenerType doesn't specify a class or interface that implements java.util.EventListener.

getSelectedText

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

Returns: The currently selected text region.

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.

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.

getText

public String getText()
Returns the text in this component

Returns: The text in this component.

getTextListeners

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

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.

paramString

protected String paramString()
Returns a debugging string.

Returns: A debugging string.

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.

Parameters: event The event to process.

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.

removeNotify

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

removeTextListener

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

Parameters: listener The listener to remove.

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.

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

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.

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.

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.

setText

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

Parameters: text The new text for this component.