javax.swing

Class JTextField

public class JTextField extends JTextComponent implements SwingConstants

Nested Class Summary
protected classJTextField.AccessibleJTextField
AccessibleJTextField
Field Summary
static StringnotifyAction
Name of the action that gets sent when the content of the text field gets accepted.
Constructor Summary
JTextField()
Creates a new instance of JTextField.
JTextField(String text)
Creates a new instance of JTextField.
JTextField(int columns)
Creates a new instance of JTextField.
JTextField(String text, int columns)
Creates a new instance of JTextField.
JTextField(Document doc, String text, int columns)
Creates a new instance of JTextField.
Method Summary
voidaddActionListener(ActionListener listener)
Adds a new listener object to this text field.
protected voidconfigurePropertiesFromAction(Action action)
protected PropertyChangeListenercreateActionPropertyChangeListener(Action action)
protected DocumentcreateDefaultModel()
Creates the default model for this text field.
protected voidfireActionPerformed()
Sends an action event to all registered ActionListener objects.
AccessibleContextgetAccessibleContext()
Returns the accessible context associated with the JTextField.
ActiongetAction()
ActionListener[]getActionListeners()
Returns all registered ActionListener objects.
Action[]getActions()
Returns the set of Actions that are commands for the editor.
intgetColumns()
Returns the number of columns of this text field.
protected intgetColumnWidth()
Returns the column width, which is the width of the character m for the font in use.
intgetHorizontalAlignment()
Returns the horizontal alignment, which is one of: JTextField.LEFT, JTextField.CENTER, JTextField.RIGHT, JTextField.LEADING, JTextField.TRAILING.
BoundedRangeModelgetHorizontalVisibility()
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.
DimensiongetPreferredSize()
Returns the preferred size.
intgetScrollOffset()
Returns the scroll offset in pixels.
StringgetUIClassID()
Returns the class ID for the UI.
booleanisValidateRoot()
Returns true, unless this is embedded in a JViewport in which case the viewport takes responsibility of validating.
voidpostActionEvent()
voidremoveActionListener(ActionListener listener)
Removes a listener object from this text field.
voidscrollRectToVisible(Rectangle r)
voidsetAction(Action newAction)
voidsetActionCommand(String command)
Sets the command string used in action events.
voidsetColumns(int columns)
Sets the number of columns and then invalidates the layout.
voidsetDocument(Document doc)
Sets the document to be used for this JTextField.
voidsetFont(Font newFont)
Sets the current font and revalidates so the font will take effect.
voidsetHorizontalAlignment(int newAlign)
Sets the horizontal alignment of the text.
voidsetScrollOffset(int offset)
Sets the scroll offset in pixels.

Field Detail

notifyAction

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

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

addActionListener

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

Parameters: listener the listener to add

configurePropertiesFromAction

protected void configurePropertiesFromAction(Action action)

Since: 1.3

createActionPropertyChangeListener

protected PropertyChangeListener createActionPropertyChangeListener(Action action)

Since: 1.3

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

fireActionPerformed

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

getAccessibleContext

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

Returns: the accessible context associated with the JTextField

getAction

public Action getAction()

Since: 1.3

getActionListeners

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

Returns: an array of listeners

Since: 1.4

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

getColumns

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

Returns: the number of columns

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.

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

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

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

getScrollOffset

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

Returns: the scroll offset

getUIClassID

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

Returns: "TextFieldUI";

isValidateRoot

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

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

postActionEvent

public void postActionEvent()

removeActionListener

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

Parameters: listener the listener to remove

scrollRectToVisible

public void scrollRectToVisible(Rectangle r)

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

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

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.

Parameters: doc the document to set

setFont

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

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.

setScrollOffset

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

Parameters: offset the scroll offset