javax.swing
Class JTextField

java.lang.Object
  extended by java.awt.Component
      extended by java.awt.Container
          extended by javax.swing.JComponent
              extended by javax.swing.text.JTextComponent
                  extended by javax.swing.JTextField
All Implemented Interfaces:
ImageObserver, MenuContainer, Serializable, Accessible, Scrollable, SwingConstants
Direct Known Subclasses:
DefaultTreeCellEditor.DefaultTextField, JFormattedTextField, JPasswordField

public class JTextField
extends JTextComponent
implements SwingConstants

See Also:
Serialized Form

Nested Class Summary
protected  class JTextField.AccessibleJTextField
          AccessibleJTextField
 
Nested classes/interfaces inherited from class javax.swing.text.JTextComponent
JTextComponent.AccessibleJTextComponent, JTextComponent.KeyBinding
 
Nested classes/interfaces inherited from class javax.swing.JComponent
JComponent.AccessibleJComponent
 
Nested classes/interfaces inherited from class java.awt.Container
Container.AccessibleAWTContainer
 
Nested classes/interfaces inherited from class java.awt.Component
Component.AccessibleAWTComponent, Component.BltBufferStrategy, Component.FlipBufferStrategy
 
Field Summary
static String notifyAction
          Name of the action that gets sent when the content of the text field gets accepted.
 
Fields inherited from class javax.swing.text.JTextComponent
DEFAULT_KEYMAP, FOCUS_ACCELERATOR_KEY
 
Fields inherited from class javax.swing.JComponent
accessibleContext, listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW
 
Fields inherited from class java.awt.Component
BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
 
Fields inherited from interface javax.swing.SwingConstants
BOTTOM, CENTER, EAST, HORIZONTAL, LEADING, LEFT, NEXT, NORTH, NORTH_EAST, NORTH_WEST, PREVIOUS, RIGHT, SOUTH, SOUTH_EAST, SOUTH_WEST, TOP, TRAILING, VERTICAL, WEST
 
Fields inherited from interface java.awt.image.ImageObserver
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
 
Constructor Summary
JTextField()
          Creates a new instance of JTextField.
JTextField(Document doc, String text, int columns)
          Creates a new instance of JTextField.
JTextField(int columns)
          Creates a new instance of JTextField.
JTextField(String text)
          Creates a new instance of JTextField.
JTextField(String text, int columns)
          Creates a new instance of JTextField.
 
Method Summary
 void addActionListener(ActionListener listener)
          Adds a new listener object to this text field.
protected  void configurePropertiesFromAction(Action action)
           
protected  PropertyChangeListener createActionPropertyChangeListener(Action action)
           
protected  Document createDefaultModel()
          Creates the default model for this text field.
protected  void fireActionPerformed()
          Sends an action event to all registered ActionListener objects.
 AccessibleContext getAccessibleContext()
          Returns the accessible context associated with the JTextField.
 Action getAction()
           
 ActionListener[] getActionListeners()
          Returns all registered ActionListener objects.
 Action[] getActions()
          Returns the set of Actions that are commands for the editor.
 int getColumns()
          Returns the number of columns of this text field.
protected  int getColumnWidth()
          Returns the column width, which is the width of the character m for the font in use.
 int getHorizontalAlignment()
          Returns the horizontal alignment, which is one of: JTextField.LEFT, JTextField.CENTER, JTextField.RIGHT, JTextField.LEADING, JTextField.TRAILING.
 BoundedRangeModel getHorizontalVisibility()
          Returns the bounded range model that describes the horizontal visibility of the text field in the case when the text does not fit into the available space.
 Dimension getPreferredSize()
          Returns the preferred size.
 int getScrollOffset()
          Returns the scroll offset in pixels.
 String getUIClassID()
          Returns the class ID for the UI.
 boolean isValidateRoot()
          Returns true, unless this is embedded in a JViewport in which case the viewport takes responsibility of validating.
 void postActionEvent()
           
 void removeActionListener(ActionListener listener)
          Removes a listener object from this text field.
 void scrollRectToVisible(Rectangle r)
          Calls scrollRectToVisible on the component's parent.
 void setAction(Action newAction)
           
 void setActionCommand(String command)
          Sets the command string used in action events.
 void setColumns(int columns)
          Sets the number of columns and then invalidates the layout.
 void setDocument(Document doc)
          Sets the document to be used for this JTextField.
 void setFont(Font newFont)
          Sets the current font and revalidates so the font will take effect.
 void setHorizontalAlignment(int newAlign)
          Sets the horizontal alignment of the text.
 void setScrollOffset(int offset)
          Sets the scroll offset in pixels.
 
Methods inherited from class javax.swing.text.JTextComponent
addCaretListener, addInputMethodListener, addKeymap, copy, cut, fireCaretUpdate, getCaret, getCaretColor, getCaretListeners, getCaretPosition, getDisabledTextColor, getDocument, getDragEnabled, getFocusAccelerator, getHighlighter, getInputMethodListeners, getKeymap, getKeymap, getMargin, getNavigationFilter, getPreferredScrollableViewportSize, getScrollableBlockIncrement, getScrollableTracksViewportHeight, getScrollableTracksViewportWidth, getScrollableUnitIncrement, getSelectedText, getSelectedTextColor, getSelectionColor, getSelectionEnd, getSelectionStart, getText, getText, getToolTipText, getUI, isEditable, loadKeymap, modelToView, moveCaretPosition, paramString, paste, read, removeCaretListener, removeInputMethodListener, removeKeymap, replaceSelection, select, selectAll, setCaret, setCaretColor, setCaretPosition, setDisabledTextColor, setDragEnabled, setEditable, setFocusAccelerator, setHighlighter, setKeymap, setMargin, setNavigationFilter, setSelectedTextColor, setSelectionColor, setSelectionEnd, setSelectionStart, setText, setUI, updateUI, viewToModel, write
 
Methods inherited from class javax.swing.JComponent
addAncestorListener, addNotify, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, firePropertyChange, fireVetoableChange, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBorder, getBounds, getClientProperty, getComponentGraphics, getComponentPopupMenu, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getGraphics, getHeight, getInheritsPopupMenu, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getListeners, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getTopLevelAncestor, getTransferHandler, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, isPaintingTile, isRequestFocusEnabled, paint, paintBorder, paintChildren, paintComponent, paintImmediately, paintImmediately, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processKeyBinding, processKeyEvent, processMouseMotionEvent, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeNotify, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, resetKeyboardActions, reshape, revalidate, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setComponentPopupMenu, setDebugGraphicsOptions, setDefaultLocale, setDoubleBuffered, setEnabled, setForeground, setInheritsPopupMenu, setInputMap, setInputVerifier, setNextFocusableComponent, setOpaque, setRequestFocusEnabled, setToolTipText, setTransferHandler, setUI, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, update
 
Methods inherited from class java.awt.Container
add, add, add, add, add, addContainerListener, addImpl, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getComponentZOrder, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getLayout, getMousePosition, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicyProvider, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paintComponents, preferredSize, printComponents, processContainerEvent, processEvent, remove, remove, removeAll, removeContainerListener, setComponentZOrder, setFocusCycleRoot, setFocusTraversalKeys, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setLayout, transferFocusDownCycle, validate, validateTree
 
Methods inherited from class java.awt.Component
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, checkImage, checkImage, coalesceEvents, contains, createImage, createImage, createVolatileImage, createVolatileImage, disableEvents, dispatchEvent, enable, enableEvents, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getBackground, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getFontMetrics, getForeground, getGraphicsConfiguration, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodRequests, getKeyListeners, getLocale, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPeer, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getToolkit, getTreeLock, gotFocus, handleEvent, hasFocus, hide, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isPreferredSizeSet, isShowing, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, postEvent, prepareImage, prepareImage, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processMouseEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, resize, resize, setBounds, setBounds, setComponentOrientation, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, setLocation, setLocation, setMaximumSize, setMinimumSize, setName, setPreferredSize, setSize, setSize, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycle
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

notifyAction

public static final String notifyAction
Name of the action that gets sent when the content of the text field gets accepted.

See Also:
Constant Field Values
Constructor Detail

JTextField

public JTextField()
Creates a new instance of JTextField.


JTextField

public JTextField(String text)
Creates a new instance of JTextField.

Parameters:
text - the initial text

JTextField

public JTextField(int columns)
Creates a new instance of JTextField.

Parameters:
columns - the number of columns
Throws:
IllegalArgumentException - if columns %lt; 0

JTextField

public JTextField(String text,
                  int columns)
Creates a new instance of JTextField.

Parameters:
text - the initial text
columns - the number of columns
Throws:
IllegalArgumentException - if columns %lt; 0

JTextField

public JTextField(Document doc,
                  String text,
                  int columns)
Creates a new instance of JTextField.

Parameters:
doc - the document to use
text - the initial text
columns - the number of columns
Throws:
IllegalArgumentException - if columns %lt; 0
Method Detail

createDefaultModel

protected Document createDefaultModel()
Creates the default model for this text field. This implementation returns an instance of PlainDocument.

Returns:
a new instance of the default model

setDocument

public void setDocument(Document doc)
Sets the document to be used for this JTextField. This sets the document property filterNewlines to true and then calls the super behaviour to setup a view and revalidate the text field.

Overrides:
setDocument in class JTextComponent
Parameters:
doc - the document to set

getUIClassID

public String getUIClassID()
Returns the class ID for the UI.

Overrides:
getUIClassID in class JTextComponent
Returns:
"TextFieldUI";
See Also:
JComponent.setUI(javax.swing.plaf.ComponentUI), JComponent.updateUI()

addActionListener

public void addActionListener(ActionListener listener)
Adds a new listener object to this text field.

Parameters:
listener - the listener to add

removeActionListener

public void removeActionListener(ActionListener listener)
Removes a listener object from this text field.

Parameters:
listener - the listener to remove

getActionListeners

public ActionListener[] getActionListeners()
Returns all registered ActionListener objects.

Returns:
an array of listeners
Since:
1.4

fireActionPerformed

protected void fireActionPerformed()
Sends an action event to all registered ActionListener objects.


getColumns

public int getColumns()
Returns the number of columns of this text field.

Returns:
the number of columns

setColumns

public void setColumns(int columns)
Sets the number of columns and then invalidates the layout.

Parameters:
columns - the number of columns
Throws:
IllegalArgumentException - if columns < 0

getHorizontalAlignment

public int getHorizontalAlignment()
Returns the horizontal alignment, which is one of: JTextField.LEFT, JTextField.CENTER, JTextField.RIGHT, JTextField.LEADING, JTextField.TRAILING.

Returns:
the horizontal alignment

setHorizontalAlignment

public void setHorizontalAlignment(int newAlign)
Sets the horizontal alignment of the text. Calls invalidate and repaint and fires a property change event.

Parameters:
newAlign - must be one of: JTextField.LEFT, JTextField.CENTER, JTextField.RIGHT, JTextField.LEADING, JTextField.TRAILING.
Throws:
IllegalArgumentException - if newAlign is not one of the above.

setFont

public void setFont(Font newFont)
Sets the current font and revalidates so the font will take effect.

Overrides:
setFont in class JComponent
Parameters:
newFont - The new value of the property
See Also:
Component.getFont()

getPreferredSize

public Dimension getPreferredSize()
Returns the preferred size. If there is a non-zero number of columns, this is the number of columns multiplied by the column width, otherwise it returns super.getPreferredSize().

Overrides:
getPreferredSize in class JComponent
Returns:
The preferred size of the component
See Also:
Component.setPreferredSize(java.awt.Dimension), Component.getPreferredSize(), Component.isPreferredSizeSet(), ComponentUI.getPreferredSize(JComponent)

getScrollOffset

public int getScrollOffset()
Returns the scroll offset in pixels.

Returns:
the scroll offset

setScrollOffset

public void setScrollOffset(int offset)
Sets the scroll offset in pixels.

Parameters:
offset - the scroll offset

getActions

public Action[] getActions()
Returns the set of Actions that are commands for the editor. This is the actions supported by this editor plus the actions of the UI (returned by JTextComponent.getActions()).

Overrides:
getActions in class JTextComponent
Returns:
The set of available Actions on this component's EditorKit
See Also:
TextUI.getEditorKit(javax.swing.text.JTextComponent), EditorKit.getActions()

postActionEvent

public void postActionEvent()

getAction

public Action getAction()
Since:
1.3

setAction

public void setAction(Action newAction)
Since:
1.3

setActionCommand

public void setActionCommand(String command)
Sets the command string used in action events.

Since:
1.3

createActionPropertyChangeListener

protected PropertyChangeListener createActionPropertyChangeListener(Action action)
Since:
1.3

configurePropertiesFromAction

protected void configurePropertiesFromAction(Action action)
Since:
1.3

getColumnWidth

protected int getColumnWidth()
Returns the column width, which is the width of the character m for the font in use.

Returns:
the width of the character m for the font in use.

getAccessibleContext

public AccessibleContext getAccessibleContext()
Returns the accessible context associated with the JTextField.

Specified by:
getAccessibleContext in interface Accessible
Overrides:
getAccessibleContext in class JTextComponent
Returns:
the accessible context associated with the JTextField

getHorizontalVisibility

public BoundedRangeModel getHorizontalVisibility()
Returns the bounded range model that describes the horizontal visibility of the text field in the case when the text does not fit into the available space. The actual values of this model are managed by the look and feel implementation.

Returns:
the bounded range model that describes the horizontal visibility

isValidateRoot

public boolean isValidateRoot()
Returns true, unless this is embedded in a JViewport in which case the viewport takes responsibility of validating.

Overrides:
isValidateRoot in class JComponent
Returns:
true, unless this is embedded in a JViewport in which case the viewport takes responsibility of validating

scrollRectToVisible

public void scrollRectToVisible(Rectangle r)
Description copied from class: JComponent
Calls scrollRectToVisible on the component's parent. Components which can service this call should override.

Overrides:
scrollRectToVisible in class JComponent
Parameters:
r - The rectangle to make visible