TextComponent (Java 2 Platform SE 5.0) (original) (raw)


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


Nested Class Summary
protected class TextComponent.AccessibleAWTTextComponent This class implements accessibility support for the TextComponent class.
Nested classes/interfaces inherited from class java.awt.Component
Component.AccessibleAWTComponent, Component.BltBufferStrategy, Component.FlipBufferStrategy
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() Returns the position of the text insertion caret.
InputMethodRequests getInputMethodRequests() Gets the input method request handler which supports requests from input methods for this component.
<T extends EventListener> T[] getListeners(Class listenerType) Returns an array of all the objects currently registered as _Foo_Listeners upon this TextComponent.
String getSelectedText() Returns 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() Returns the text that is presented by this text component.
TextListener[] getTextListeners() Returns an array of all the text listeners registered on this text component.
boolean isEditable() Indicates whether or not this text component is editable.
protected String paramString() Returns a string representing the state of thisTextComponent.
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](../../java/awt/TextComponent.html#select%28int, int%29)(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.
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](../../java/awt/Component.html#action%28java.awt.Event, java.lang.Object%29), add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, addPropertyChangeListener, [addPropertyChangeListener](../../java/awt/Component.html#addPropertyChangeListener%28java.lang.String, java.beans.PropertyChangeListener%29), applyComponentOrientation, areFocusTraversalKeysSet, bounds, [checkImage](../../java/awt/Component.html#checkImage%28java.awt.Image, java.awt.image.ImageObserver%29), [checkImage](../../java/awt/Component.html#checkImage%28java.awt.Image, int, int, java.awt.image.ImageObserver%29), [coalesceEvents](../../java/awt/Component.html#coalesceEvents%28java.awt.AWTEvent, java.awt.AWTEvent%29), [contains](../../java/awt/Component.html#contains%28int, int%29), contains, createImage, [createImage](../../java/awt/Component.html#createImage%28int, int%29), [createVolatileImage](../../java/awt/Component.html#createVolatileImage%28int, int%29), [createVolatileImage](../../java/awt/Component.html#createVolatileImage%28int, int, java.awt.ImageCapabilities%29), deliverEvent, disable, disableEvents, dispatchEvent, doLayout, enable, enable, enableEvents, [firePropertyChange](../../java/awt/Component.html#firePropertyChange%28java.lang.String, boolean, boolean%29), [firePropertyChange](../../java/awt/Component.html#firePropertyChange%28java.lang.String, byte, byte%29), [firePropertyChange](../../java/awt/Component.html#firePropertyChange%28java.lang.String, char, char%29), [firePropertyChange](../../java/awt/Component.html#firePropertyChange%28java.lang.String, double, double%29), [firePropertyChange](../../java/awt/Component.html#firePropertyChange%28java.lang.String, float, float%29), [firePropertyChange](../../java/awt/Component.html#firePropertyChange%28java.lang.String, int, int%29), [firePropertyChange](../../java/awt/Component.html#firePropertyChange%28java.lang.String, long, long%29), [firePropertyChange](../../java/awt/Component.html#firePropertyChange%28java.lang.String, java.lang.Object, java.lang.Object%29), [firePropertyChange](../../java/awt/Component.html#firePropertyChange%28java.lang.String, short, short%29), getAlignmentX, getAlignmentY, getBounds, getBounds, getColorModel, [getComponentAt](../../java/awt/Component.html#getComponentAt%28int, int%29), getComponentAt, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeys, getFocusTraversalKeysEnabled, getFont, getFontMetrics, getForeground, getGraphics, getGraphicsConfiguration, getHeight, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, 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](../../java/awt/Component.html#gotFocus%28java.awt.Event, java.lang.Object%29), handleEvent, hasFocus, hide, [imageUpdate](../../java/awt/Component.html#imageUpdate%28java.awt.Image, int, int, int, int, int%29), [inside](../../java/awt/Component.html#inside%28int, int%29), invalidate, isBackgroundSet, isCursorSet, isDisplayable, isDoubleBuffered, isEnabled, isFocusable, isFocusCycleRoot, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isOpaque, isPreferredSizeSet, isShowing, isValid, isVisible, [keyDown](../../java/awt/Component.html#keyDown%28java.awt.Event, int%29), [keyUp](../../java/awt/Component.html#keyUp%28java.awt.Event, int%29), layout, list, list, [list](../../java/awt/Component.html#list%28java.io.PrintStream, int%29), list, [list](../../java/awt/Component.html#list%28java.io.PrintWriter, int%29), [locate](../../java/awt/Component.html#locate%28int, int%29), location, [lostFocus](../../java/awt/Component.html#lostFocus%28java.awt.Event, java.lang.Object%29), minimumSize, [mouseDown](../../java/awt/Component.html#mouseDown%28java.awt.Event, int, int%29), [mouseDrag](../../java/awt/Component.html#mouseDrag%28java.awt.Event, int, int%29), [mouseEnter](../../java/awt/Component.html#mouseEnter%28java.awt.Event, int, int%29), [mouseExit](../../java/awt/Component.html#mouseExit%28java.awt.Event, int, int%29), [mouseMove](../../java/awt/Component.html#mouseMove%28java.awt.Event, int, int%29), [mouseUp](../../java/awt/Component.html#mouseUp%28java.awt.Event, int, int%29), [move](../../java/awt/Component.html#move%28int, int%29), nextFocus, paint, paintAll, postEvent, preferredSize, [prepareImage](../../java/awt/Component.html#prepareImage%28java.awt.Image, java.awt.image.ImageObserver%29), [prepareImage](../../java/awt/Component.html#prepareImage%28java.awt.Image, int, int, java.awt.image.ImageObserver%29), print, printAll, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processKeyEvent, processMouseEvent, processMouseMotionEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, [removePropertyChangeListener](../../java/awt/Component.html#removePropertyChangeListener%28java.lang.String, java.beans.PropertyChangeListener%29), repaint, [repaint](../../java/awt/Component.html#repaint%28int, int, int, int%29), repaint, [repaint](../../java/awt/Component.html#repaint%28long, int, int, int, int%29), requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, [reshape](../../java/awt/Component.html#reshape%28int, int, int, int%29), resize, [resize](../../java/awt/Component.html#resize%28int, int%29), [setBounds](../../java/awt/Component.html#setBounds%28int, int, int, int%29), setBounds, setComponentOrientation, setCursor, setDropTarget, setEnabled, setFocusable, [setFocusTraversalKeys](../../java/awt/Component.html#setFocusTraversalKeys%28int, java.util.Set%29), setFocusTraversalKeysEnabled, setFont, setForeground, setIgnoreRepaint, setLocale, [setLocation](../../java/awt/Component.html#setLocation%28int, int%29), setLocation, setMaximumSize, setMinimumSize, setName, setPreferredSize, setSize, [setSize](../../java/awt/Component.html#setSize%28int, int%29), 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](../../java/lang/Object.html#wait%28long, int%29)
Field Detail

textListener

protected transient TextListener textListener

Method Detail

enableInputMethods

public void enableInputMethods(boolean enable)

Description copied from class: [Component](../../java/awt/Component.html#enableInputMethods%28boolean%29)

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](../../java/awt/Component.html#enableInputMethods%28boolean%29) in class [Component](../../java/awt/Component.html "class in java.awt")

Parameters:

enable - true to enable, false to disable

See Also:

Component.processKeyEvent(java.awt.event.KeyEvent)


getInputMethodRequests

public InputMethodRequests getInputMethodRequests()

Description copied from class: [Component](../../java/awt/Component.html#getInputMethodRequests%28%29)

Gets the input method request handler which supports requests from input methods for this component. A component that supports on-the-spot text input must override this method to return an InputMethodRequests instance. At the same time, it also has to handle input method events.

Overrides:

[getInputMethodRequests](../../java/awt/Component.html#getInputMethodRequests%28%29) in class [Component](../../java/awt/Component.html "class in java.awt")

Returns:

the input method request handler for this component,null by default

See Also:

Component.addInputMethodListener(java.awt.event.InputMethodListener)


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](../../java/awt/Component.html#addNotify%28%29) in class [Component](../../java/awt/Component.html "class in java.awt")

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](../../java/awt/Component.html#removeNotify%28%29) in class [Component](../../java/awt/Component.html "class in java.awt")

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()

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:

setText(java.lang.String)


getSelectedText

public String 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:

[select(int, int)](../../java/awt/TextComponent.html#select%28int, int%29)


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](../../java/awt/Component.html#getBackground%28%29) in class [Component](../../java/awt/Component.html "class in java.awt")

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(Color)


setBackground

public void setBackground(Color c)

Sets the background color of this text component.

Overrides:

[setBackground](../../java/awt/Component.html#setBackground%28java.awt.Color%29) in class [Component](../../java/awt/Component.html "class in java.awt")

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 (char value) to be selected

selectionEnd - the zero-based end position of the text to be selected; the character (char value) 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)](../../java/awt/TextComponent.html#select%28int, int%29)


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 the TextComponent contains no text) and no error is returned. If the passed-in value is less than 0, an IllegalArgumentException is thrown.

Parameters:

position - the position of the text insertion caret

Throws:

[IllegalArgumentException](../../java/lang/IllegalArgumentException.html "class in java.lang") - if position is less than zero

Since:

JDK1.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:

JDK1.1

See Also:

setCaretPosition(int)


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

See Also:

removeTextListener(java.awt.event.TextListener), getTextListeners(), TextListener


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:

addTextListener(java.awt.event.TextListener), getTextListeners(), TextListener


getTextListeners

public TextListener[] getTextListeners()

Returns an array of all the text listeners registered on this text component.

Returns:

all of this text component's TextListeners or an empty array if no text listeners are currently registered

Since:

1.4

See Also:

addTextListener(java.awt.event.TextListener), removeTextListener(java.awt.event.TextListener)


getListeners

public <T extends EventListener> T[] getListeners(Class listenerType)

Returns an array of all the objects currently registered as _Foo_Listeners upon this TextComponent._Foo_Listeners are registered using theadd_Foo_Listener method.

You can specify the listenerType argument with a class literal, such as_Foo_Listener.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](../../java/awt/Component.html#getListeners%28java.lang.Class%29) in class [Component](../../java/awt/Component.html "class in java.awt")

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_Foo_Listeners on this text component, or an empty array if no such listeners have been added

Throws:

[ClassCastException](../../java/lang/ClassCastException.html "class in java.lang") - if listenerType doesn't specify a class or interface that implementsjava.util.EventListener

Since:

1.3

See Also:

getTextListeners()


processEvent

protected void processEvent(AWTEvent e)

Processes events on this text component. If the event is aTextEvent, it invokes the processTextEvent method else it invokes its superclass's processEvent.

Note that if the event parameter is null the behavior is unspecified and may result in an exception.

Overrides:

[processEvent](../../java/awt/Component.html#processEvent%28java.awt.AWTEvent%29) in class [Component](../../java/awt/Component.html "class in java.awt")

Parameters:

e - the event

See Also:

Component.processComponentEvent(java.awt.event.ComponentEvent), Component.processFocusEvent(java.awt.event.FocusEvent), Component.processKeyEvent(java.awt.event.KeyEvent), Component.processMouseEvent(java.awt.event.MouseEvent), Component.processMouseMotionEvent(java.awt.event.MouseEvent), Component.processInputMethodEvent(java.awt.event.InputMethodEvent), Component.processHierarchyEvent(java.awt.event.HierarchyEvent), Component.processMouseWheelEvent(java.awt.event.MouseWheelEvent)


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:

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:

Component.enableEvents(long)


paramString

protected String 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 be null.

Overrides:

[paramString](../../java/awt/Component.html#paramString%28%29) in class [Component](../../java/awt/Component.html "class in java.awt")

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](../../javax/accessibility/Accessible.html#getAccessibleContext%28%29) in interface [Accessible](../../javax/accessibility/Accessible.html "interface in javax.accessibility")

Overrides:

[getAccessibleContext](../../java/awt/Component.html#getAccessibleContext%28%29) in class [Component](../../java/awt/Component.html "class in java.awt")

Returns:

an AccessibleAWTTextComponent that serves as the AccessibleContext of this TextComponent



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.

Copyright © 2004, 2010 Oracle and/or its affiliates. All rights reserved. Use is subject to license terms. Also see the documentation redistribution policy.