Class TextField
- java.lang.Object
-
- java.awt.Component
-
- java.awt.TextComponent
-
- java.awt.TextField
-
- All Implemented Interfaces:
- java.awt.image.ImageObserver, MenuContainer, java.io.Serializable, javax.accessibility.Accessible
public class TextField extends TextComponentATextField
object is a text component that allows for the editing of a single line of text.For example, the following image depicts a frame with four text fields of varying widths. Two of these text fields display the predefined text
"Hello"
.Here is the code that produces these four text fields:
TextField tf1, tf2, tf3, tf4; // a blank text field tf1 = new TextField(); // blank field of 20 columns tf2 = new TextField("", 20); // predefined text displayed tf3 = new TextField("Hello!"); // predefined text in 30 columns tf4 = new TextField("Hello", 30);
Every time the user types a key in the text field, one or more key events are sent to the text field. A
KeyEvent
may be one of three types: keyPressed, keyReleased, or keyTyped. The properties of a key event indicate which of these types it is, as well as additional information about the event, such as what modifiers are applied to the key event and the time at which the event occurred.The key event is passed to every
KeyListener
orKeyAdapter
object which registered to receive such events using the component'saddKeyListener
method. (KeyAdapter
objects implement theKeyListener
interface.)It is also possible to fire an
ActionEvent
. If action events are enabled for the text field, they may be fired by pressing theReturn
key.The
TextField
class'sprocessEvent
method examines the action event and passes it along toprocessActionEvent
. The latter method redirects the event to anyActionListener
objects that have registered to receive action events generated by this text field.
-
-
Nested Classes Modifier and Type Class and Description AccessibleAWTComponent Inner class of Component used to provide default support for accessibility.BaselineResizeBehavior Enumeration of the common ways the baseline of a component can change as the size changes.BltBufferStrategy Inner class for blitting offscreen surfaces to a component.FlipBufferStrategy Inner class for flipping buffers on a component.AccessibleAWTTextComponent This class implements accessibility support for theTextComponent
class.AccessibleAWTTextField This class implements accessibility support for theTextField
class.
-
Fields Modifier and Type Field and Description ABORT This flag in the infoflags argument to imageUpdate indicates that an image which was being tracked asynchronously was aborted before production was complete.ALLBITS This flag in the infoflags argument to imageUpdate indicates that a static image which was previously drawn is now complete and can be drawn again in its final form.BOTTOM_ALIGNMENT Ease-of-use constant forgetAlignmentY
.CENTER_ALIGNMENT Ease-of-use constant forgetAlignmentY
andgetAlignmentX
.ERROR This flag in the infoflags argument to imageUpdate indicates that an image which was being tracked asynchronously has encountered an error.FRAMEBITS This flag in the infoflags argument to imageUpdate indicates that another complete frame of a multi-frame image which was previously drawn is now available to be drawn again.HEIGHT This flag in the infoflags argument to imageUpdate indicates that the height of the base image is now available and can be taken from the height argument to the imageUpdate callback method.LEFT_ALIGNMENT Ease-of-use constant forgetAlignmentX
.PROPERTIES This flag in the infoflags argument to imageUpdate indicates that the properties of the image are now available.RIGHT_ALIGNMENT Ease-of-use constant forgetAlignmentX
.SOMEBITS This flag in the infoflags argument to imageUpdate indicates that more pixels needed for drawing a scaled variation of the image are available.textListener TOP_ALIGNMENT Ease-of-use constant forgetAlignmentY()
.WIDTH This flag in the infoflags argument to imageUpdate indicates that the width of the base image is now available and can be taken from the width argument to the imageUpdate callback method.
-
-
Basic Attribute Methods Modifier and Type Method and Description applyComponentOrientation Sets theComponentOrientation
property of this component and all components contained within it.disable Deprecated. As of JDK version 1.1, replaced bysetEnabled(boolean)
.enable Deprecated. As of JDK version 1.1, replaced bysetEnabled(boolean)
.enable Deprecated. As of JDK version 1.1, replaced bysetEnabled(boolean)
.enableInputMethods Enables or disables input method support for this text component.getAccessibleContext Gets the AccessibleContext associated with this TextField.getBackground Gets the background color of this text component.getColorModel Gets the instance ofColorModel
used to display the component on the output device.getComponentOrientation Retrieves the language-sensitive orientation that is to be used to order the elements or text within this component.getCursor Gets the cursor set in the component.getFont Gets the font of this component.getForeground Gets the foreground color of this component.getLocale Gets the locale of this component.getName Gets the name of the component.getParent Gets the parent of this component.hide Deprecated. As of JDK version 1.1, replaced bysetVisible(boolean)
.isBackgroundSet Returns whether the background color has been explicitly set for this Component.isCursorSet Returns whether the cursor has been explicitly set for this Component.isDisplayable Determines whether this component is displayable.isDoubleBuffered Returns true if this component is painted to an offscreen image ("buffer") that's copied to the screen later.isEnabled Determines whether this component is enabled.isFontSet Returns whether the font has been explicitly set for this Component.isForegroundSet Returns whether the foreground color has been explicitly set for this Component.isLightweight A lightweight component doesn't have a native toolkit peer.isOpaque Returns true if this component is completely opaque, returns false by default.isShowing Determines whether this component is showing on screen.isVisible Determines whether this component should be visible when its parent is visible.setBackground Sets the background color of this text component.setComponentOrientation Sets the language-sensitive orientation that is to be used to order the elements or text within this component.setCursor Sets the cursor image to the specified cursor.setEnabled Enables or disables this component, depending on the value of the parameterb
.setFont Sets the font of this component.setForeground Sets the foreground color of this component.setLocale Sets the locale of this component.setName Sets the name of the component to the specified string.setVisible Shows or hides this component depending on the value of parameterb
.show Deprecated. As of JDK version 1.1, replaced bysetVisible(boolean)
.show Deprecated. As of JDK version 1.1, replaced bysetVisible(boolean)
. -
Event Support Methods Modifier and Type Method and Description action Deprecated. As of JDK version 1.1, should register this component as ActionListener on component which fires action events.coalesceEvents Potentially coalesce an event being posted with an existing event.deliverEvent Deprecated. As of JDK version 1.1, replaced bydispatchEvent(AWTEvent e)
.disableEvents Disables the events defined by the specified event mask parameter from being delivered to this component.dispatchEvent Dispatches an event to this component or one of its sub components.enableEvents Enables the events defined by the specified event mask parameter to be delivered to this component.firePropertyChange Support for reporting bound property changes for boolean properties.firePropertyChange Reports a bound property change.firePropertyChange Reports a bound property change.firePropertyChange Reports a bound property change.firePropertyChange Reports a bound property change.firePropertyChange Support for reporting bound property changes for integer properties.firePropertyChange Reports a bound property change.firePropertyChange Support for reporting bound property changes for Object properties.firePropertyChange Reports a bound property change.getListeners Returns an array of all the objects currently registered asFooListener
s upon thisTextField
.handleEvent Deprecated. As of JDK version 1.1 replaced by processEvent(AWTEvent).keyDown Deprecated. As of JDK version 1.1, replaced by processKeyEvent(KeyEvent).keyUp Deprecated. As of JDK version 1.1, replaced by processKeyEvent(KeyEvent).mouseDown Deprecated. As of JDK version 1.1, replaced by processMouseEvent(MouseEvent).mouseDrag Deprecated. As of JDK version 1.1, replaced by processMouseMotionEvent(MouseEvent).mouseEnter Deprecated. As of JDK version 1.1, replaced by processMouseEvent(MouseEvent).mouseExit Deprecated. As of JDK version 1.1, replaced by processMouseEvent(MouseEvent).mouseMove Deprecated. As of JDK version 1.1, replaced by processMouseMotionEvent(MouseEvent).mouseUp Deprecated. As of JDK version 1.1, replaced by processMouseEvent(MouseEvent).postEvent Deprecated. As of JDK version 1.1, replaced by dispatchEvent(AWTEvent).processComponentEvent Processes component events occurring on this component by dispatching them to any registeredComponentListener
objects.processEvent Processes events on this text field.processFocusEvent Processes focus events occurring on this component by dispatching them to any registeredFocusListener
objects.processHierarchyBoundsEvent Processes hierarchy bounds events occurring on this component by dispatching them to any registeredHierarchyBoundsListener
objects.processHierarchyEvent Processes hierarchy events occurring on this component by dispatching them to any registeredHierarchyListener
objects.processInputMethodEvent Processes input method events occurring on this component by dispatching them to any registeredInputMethodListener
objects.processKeyEvent Processes key events occurring on this component by dispatching them to any registeredKeyListener
objects.processMouseEvent Processes mouse events occurring on this component by dispatching them to any registeredMouseListener
objects.processMouseMotionEvent Processes mouse motion events occurring on this component by dispatching them to any registeredMouseMotionListener
objects.processMouseWheelEvent Processes mouse wheel events occurring on this component by dispatching them to any registeredMouseWheelListener
objects. -
Event Registration Methods Modifier and Type Method and Description addComponentListener Adds the specified component listener to receive component events from this component.addFocusListener Adds the specified focus listener to receive focus events from this component when this component gains input focus.addHierarchyBoundsListener Adds the specified hierarchy bounds listener to receive hierarchy bounds events from this component when the hierarchy to which this container belongs changes.addHierarchyListener Adds the specified hierarchy listener to receive hierarchy changed events from this component when the hierarchy to which this container belongs changes.addInputMethodListener Adds the specified input method listener to receive input method events from this component.addKeyListener Adds the specified key listener to receive key events from this component.addMouseListener Adds the specified mouse listener to receive mouse events from this component.addMouseMotionListener Adds the specified mouse motion listener to receive mouse motion events from this component.addMouseWheelListener Adds the specified mouse wheel listener to receive mouse wheel events from this component.addPropertyChangeListener Adds a PropertyChangeListener to the listener list.addPropertyChangeListener Adds a PropertyChangeListener to the listener list for a specific property.getComponentListeners Returns an array of all the component listeners registered on this component.getFocusListeners Returns an array of all the focus listeners registered on this component.getHierarchyBoundsListeners Returns an array of all the hierarchy bounds listeners registered on this component.getHierarchyListeners Returns an array of all the hierarchy listeners registered on this component.getInputMethodListeners Returns an array of all the input method listeners registered on this component.getKeyListeners Returns an array of all the key listeners registered on this component.getMouseListeners Returns an array of all the mouse listeners registered on this component.getMouseMotionListeners Returns an array of all the mouse motion listeners registered on this component.getMouseWheelListeners Returns an array of all the mouse wheel listeners registered on this component.getPropertyChangeListeners Returns an array of all the property change listeners registered on this component.getPropertyChangeListeners Returns an array of all the listeners which have been associated with the named property.removeComponentListener Removes the specified component listener so that it no longer receives component events from this component.removeFocusListener Removes the specified focus listener so that it no longer receives focus events from this component.removeHierarchyBoundsListener Removes the specified hierarchy bounds listener so that it no longer receives hierarchy bounds events from this component.removeHierarchyListener Removes the specified hierarchy listener so that it no longer receives hierarchy changed events from this component.removeInputMethodListener Removes the specified input method listener so that it no longer receives input method events from this component.removeKeyListener Removes the specified key listener so that it no longer receives key events from this component.removeMouseListener Removes the specified mouse listener so that it no longer receives mouse events from this component.removeMouseMotionListener Removes the specified mouse motion listener so that it no longer receives mouse motion events from this component.removeMouseWheelListener Removes the specified mouse wheel listener so that it no longer receives mouse wheel events from this component.removePropertyChangeListener Removes a PropertyChangeListener from the listener list.removePropertyChangeListener Removes aPropertyChangeListener
from the listener list for a specific property. -
Keyboard Focus Methods Modifier and Type Method and Description areFocusTraversalKeysSet Returns whether the Set of focus traversal keys for the given focus traversal operation has been explicitly defined for this Component.getFocusCycleRootAncestor Returns the Container which is the focus cycle root of this Component's focus traversal cycle.getFocusTraversalKeys Returns the Set of focus traversal keys for a given traversal operation for this Component.getFocusTraversalKeysEnabled Returns whether focus traversal keys are enabled for this Component.gotFocus Deprecated. As of JDK version 1.1, replaced by processFocusEvent(FocusEvent).hasFocus Returnstrue
if thisComponent
is the focus owner.isFocusable Returns whether this Component can be focused.isFocusCycleRoot Returns whether the specified Container is the focus cycle root of this Component's focus traversal cycle.isFocusOwner Returnstrue
if thisComponent
is the focus owner.isFocusTraversable Deprecated. As of 1.4, replaced byisFocusable()
.lostFocus Deprecated. As of JDK version 1.1, replaced by processFocusEvent(FocusEvent).nextFocus Deprecated. As of JDK version 1.1, replaced by transferFocus().requestFocus Requests that this Component get the input focus, and that this Component's top-level ancestor become the focused Window.requestFocus Requests that thisComponent
get the input focus, and that thisComponent
's top-level ancestor become the focusedWindow
.requestFocusInWindow Requests that this Component get the input focus, if this Component's top-level ancestor is already the focused Window.requestFocusInWindow Requests that thisComponent
get the input focus, if thisComponent
's top-level ancestor is already the focusedWindow
.setFocusable Sets the focusable state of this Component to the specified value.setFocusTraversalKeys Sets the focus traversal keys for a given traversal operation for this Component.setFocusTraversalKeysEnabled Sets whether focus traversal keys are enabled for this Component.transferFocus Transfers the focus to the next component, as though this Component were the focus owner.transferFocusBackward Transfers the focus to the previous component, as though this Component were the focus owner.transferFocusUpCycle Transfers the focus up one focus traversal cycle. -
Graphics Methods Modifier and Type Method and Description getFontMetrics Gets the font metrics for the specified font.getGraphics Creates a graphics context for this component.getGraphicsConfiguration Gets theGraphicsConfiguration
associated with thisComponent
.getLocationOnScreen Gets the location of this component in the form of a point specifying the component's top-left corner in the screen's coordinate space.getMousePosition Returns the position of the mouse pointer in thisComponent
's coordinate space if theComponent
is directly under the mouse pointer, otherwise returnsnull
.getToolkit Gets the toolkit of this component. -
Image Methods Modifier and Type Method and Description checkImage Returns the status of the construction of a screen representation of the specified image.checkImage Returns the status of the construction of a screen representation of the specified image.createImage Creates an image from the specified image producer.createImage Creates an off-screen drawable image to be used for double buffering.createVolatileImage Creates a volatile off-screen drawable image to be used for double buffering.createVolatileImage Creates a volatile off-screen drawable image, with the given capabilities.imageUpdate Repaints the component when the image has changed.prepareImage Prepares an image for rendering on this component.prepareImage Prepares an image for rendering on this component at the specified width and height. -
Layout Methods Modifier and Type Method and Description bounds Deprecated. As of JDK version 1.1, replaced bygetBounds()
.contains Checks whether this component "contains" the specified point, wherex
andy
are defined to be relative to the coordinate system of this component.contains Checks whether this component "contains" the specified point, where the point's x and y coordinates are defined to be relative to the coordinate system of this component.doLayout Prompts the layout manager to lay out this component.getAlignmentX Returns the alignment along the x axis.getAlignmentY Returns the alignment along the y axis.getBaseline Returns the baseline.getBaselineResizeBehavior Returns an enum indicating how the baseline of the component changes as the size changes.getBounds Gets the bounds of this component in the form of aRectangle
object.getBounds Stores the bounds of this component into "return value" rv and return rv.getComponentAt Determines if this component or one of its immediate subcomponents contains the (x, y) location, and if so, returns the containing component.getComponentAt Returns the component or subcomponent that contains the specified point.getHeight Returns the current height of this component.getLocation Gets the location of this component in the form of a point specifying the component's top-left corner.getLocation Stores the x,y origin of this component into "return value" rv and return rv.getMaximumSize Gets the maximum size of this component.getMinimumSize Gets the minumum dimensions for this text field.getPreferredSize Gets the preferred size of this text field.getSize Returns the size of this component in the form of aDimension
object.getSize Stores the width/height of this component into "return value" rv and return rv.getWidth Returns the current width of this component.getX Returns the current x coordinate of the components origin.getY Returns the current y coordinate of the components origin.inside Deprecated. As of JDK version 1.1, replaced by contains(int, int).invalidate Invalidates this component and its ancestors.isMaximumSizeSet Returns true if the maximum size has been set to a non-null
value otherwise returns false.isMinimumSizeSet Returns whether or notsetMinimumSize
has been invoked with a non-null value.isPreferredSizeSet Returns true if the preferred size has been set to a non-null
value otherwise returns false.isValid Determines whether this component is valid.layout Deprecated. As of JDK version 1.1, replaced bydoLayout()
.locate Deprecated. As of JDK version 1.1, replaced by getComponentAt(int, int).location Deprecated. As of JDK version 1.1, replaced bygetLocation()
.minimumSize Deprecated. As of JDK version 1.1, replaced bygetMinimumSize()
.move Deprecated. As of JDK version 1.1, replaced bysetLocation(int, int)
.preferredSize Deprecated. As of JDK version 1.1, replaced bygetPreferredSize()
.reshape Deprecated. As of JDK version 1.1, replaced bysetBounds(int, int, int, int)
.resize Deprecated. As of JDK version 1.1, replaced bysetSize(Dimension)
.resize Deprecated. As of JDK version 1.1, replaced bysetSize(int, int)
.revalidate Revalidates the component hierarchy up to the nearest validate root.setBounds Moves and resizes this component.setBounds Moves and resizes this component to conform to the new bounding rectangler
.setLocation Moves this component to a new location.setLocation Moves this component to a new location.setMaximumSize Sets the maximum size of this component to a constant value.setMinimumSize Sets the minimum size of this component to a constant value.setPreferredSize Sets the preferred size of this component to a constant value.setSize Resizes this component so that it has widthd.width
and heightd.height
.setSize Resizes this component so that it has widthwidth
and heightheight
.size Deprecated. As of JDK version 1.1, replaced bygetSize()
.validate Validates this component. -
Painting Methods Modifier and Type Method and Description getIgnoreRepaint paint Paints this component.paintAll Paints this component and all of its subcomponents.repaint Repaints this component.repaint Repaints the specified rectangle of this component.repaint Repaints the component.repaint Repaints the specified rectangle of this component withintm
milliseconds.setIgnoreRepaint Sets whether or not paint messages received from the operating system should be ignored.update Updates this component. -
Printing Methods Modifier and Type Method and Description list Prints a listing of this component to the standard system output streamSystem.out
.list Prints a listing of this component to the specified output stream.list Prints out a list, starting at the specified indentation, to the specified print stream.list Prints a listing to the specified print writer.list Prints out a list, starting at the specified indentation, to the specified print writer.print Prints this component.printAll Prints this component and all of its subcomponents. -
Other Methods Modifier and Type Method and Description add Adds the specified popup menu to the component.addActionListener Adds the specified action listener to receive action events from this text field.addNotify Creates the TextField's peer.addTextListener Adds the specified text event listener to receive text events from this text component.echoCharIsSet Indicates whether or not this text field has a character set for echoing.getActionListeners Returns an array of all the action listeners registered on this textfield.getCaretPosition Returns the position of the text insertion caret.getColumns Gets the number of columns in this text field.getDropTarget Gets theDropTarget
associated with thisComponent
.getEchoChar Gets the character that is to be used for echoing.getInputContext Gets the input context used by this component for handling the communication with input methods when text is entered in this component.getInputMethodRequests Gets the input method request handler which supports requests from input methods for this component.getMinimumSize Gets the minumum dimensions for a text field with the specified number of columns.getPeer Deprecated. As of JDK version 1.1, programs should not directly manipulate peers; replaced byboolean isDisplayable()
.getPreferredSize Gets the preferred size of this text field with the specified number of columns.getSelectedText Returns the selected text from the text that is presented by this text component.getSelectionEnd Gets the end position of the selected text in this text component.getSelectionStart Gets the start position of the selected text in this text component.getText Returns the text that is presented by this text component.getTextListeners Returns an array of all the text listeners registered on this text component.getTreeLock Gets this component's locking object (the object that owns the thread synchronization monitor) for AWT component-tree and layout operations.isEditable Indicates whether or not this text component is editable.minimumSize Deprecated. As of JDK version 1.1, replaced bygetMinimumSize(int)
.paramString Returns a string representing the state of thisTextField
.preferredSize Deprecated. As of JDK version 1.1, replaced bygetPreferredSize(int)
.processActionEvent Processes action events occurring on this text field by dispatching them to any registeredActionListener
objects.processTextEvent Processes text events occurring on this text component by dispatching them to any registeredTextListener
objects.remove Removes the specified popup menu from the component.removeActionListener Removes the specified action listener so that it no longer receives action events from this text field.removeNotify Removes theTextComponent
's peer.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.select Selects the text between the specified start and end positions.selectAll Selects all the text in this text component.setCaretPosition Sets the position of the text insertion caret.setColumns Sets the number of columns in this text field.setDropTarget Associate aDropTarget
with this component.setEchoChar Sets the echo character for this text field.setEchoCharacter Deprecated. As of JDK version 1.1, replaced bysetEchoChar(char)
.setEditable Sets the flag that determines whether or not this text component is editable.setSelectionEnd Sets the selection end for this text component to the specified position.setSelectionStart Sets the selection start for this text component to the specified position.setText Sets the text that is presented by this text component to be the specified text.toString Returns a string representation of this component and its values.
-
-