public class JTextField extends JTextComponent implements SwingConstants
JTextField は、1 行のテキストの編集を可能にする軽量コンポーネントです。テキストフィールドの詳細と使用例については、『The Java Tutorial』の「How to Use Text Fields」を参照してください。
JTextField は、この処理を適切に行う java.awt.TextField クラスとのソース互換性を提供するために用意されています。このコンポーネントは、java.awt.TextField クラスにはない機能を備えています。追加機能については、スーパークラスを参照してください。
JTextField は、発生するアクションイベントのコマンド文字列として使う文字列を確定するメソッドを持ちます。java.awt.TextField は、フィールドのテキストを ActionEvent のコマンド文字列として使いました。
JTextField は null でなければ、setActionCommand メソッドで設定されたコマンド文字列を使います。それ以外の場合は、フィールドのテキストを java.awt.TextField との互換機能として使います。
プラグイン可能な Look & Feel の新しい実装がパスワード文字列を偶発的に表示してしまわないようにするために、setEchoChar メソッドと getEchoChar メソッドは直接は提供されません。パスワード様式のサービスを提供するためには、これとは別の JPasswordField クラスが JTextField を拡張して導入され、独自にプラグイン可能な Look & Feel にこのサービスを提供します。
java.awt.TextField の変更を監視するには、TextEvent の TextListener を追加します。JTextComponent ベースのコンポーネントでは、DocumentEvent 経由でモデルから DocumentListeners に変更が送られます。DocumentEvent は、必要に応じて、変更位置と変更の種類を提供します。この部分のコードは次のようになります。
DocumentListener myListener = ??;
JTextField myArea = ??;
myArea.getDocument().addDocumentListener(myListener);
JTextField の水平配置は、左揃え、中央揃え、右揃え、または末尾調整に設定できます。フィールドテキストの必要なサイズがそのフィールドに割り当てられたサイズよりも小さい場合は、右揃えと末尾調整は便利です。これは setHorizontalAlignment メソッドと getHorizontalAlignment メソッドによって指定されます。デフォルトでは、左揃えになります。
テキストフィールドが VK_ENTER イベントを消費する方法は、このテキストフィールドにアクションリスナーがあるかどうかによって異なります。アクションリスナーがある場合は、VK_ENTER によりリスナーに ActionEvent が返され、VK_ENTER イベントが消費されます。これは、AWT テキストフィールドが VK_ENTER イベントを処理する方法と互換性があります。テキストフィールドにアクションリスナーがない場合、v 1.3 では、VK_ENTER イベントは消費されません。代わりに、上位クラスのコンポーネントのバインディングが処理されて、JFC/Swing のデフォルトボタン機能が使用できます。
カスタマイズされたフィールドを簡単に作成するには、モデルを拡張して、提供されるデフォルトモデルを変更します。たとえば次のコードの一部は、大文字だけを保持するフィールドを作成します。これは、テキストがクリップボードからペーストされたり、プログラムに基づいて変更されても機能します。
public class UpperCaseField extends JTextField {
public UpperCaseField(int cols) {
super(cols);
}
protected Document createDefaultModel() {
return new UpperCaseDocument();
}
static class UpperCaseDocument extends PlainDocument {
public void insertString(int offs, String str, AttributeSet a)
throws BadLocationException {
if (str == null) {
return;
}
char[] upper = str.toCharArray();
for (int i = 0; i < upper.length; i++) {
upper[i] = Character.toUpperCase(upper[i]);
}
super.insertString(offs, new String(upper), a);
}
}
}
警告: Swing はスレッドに対して安全ではありません。詳細は、「Swing's Threading Policy」を参照してください。
警告: このクラスの直列化されたオブジェクトは、今後の Swing リリースと互換ではなくなる予定です。現在の直列化のサポートは、短期間の格納や、同じバージョンの Swing を実行するアプリケーション間の RMI に適しています。1.4 以降、すべての JavaBeansTM 用の長期間の格納サポートが java.beans パッケージに追加されています。XMLEncoder を参照してください。
| 修飾子と型 | クラスと説明 |
|---|---|
protected class |
JTextField.AccessibleJTextField
このクラスは
JTextField クラスのアクセシビリティーサポートを実装しています。 |
JTextComponent.AccessibleJTextComponent, JTextComponent.DropLocation, JTextComponent.KeyBindingJComponent.AccessibleJComponentContainer.AccessibleAWTContainerComponent.AccessibleAWTComponent, Component.BaselineResizeBehavior, Component.BltBufferStrategy, Component.FlipBufferStrategy| 修飾子と型 | フィールドと説明 |
|---|---|
static String |
notifyAction
フィールドの内容が受け付けられたという通知を送るアクションの名前です。
|
DEFAULT_KEYMAP, FOCUS_ACCELERATOR_KEYaccessibleContext, listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOWBOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENTBOTTOM, CENTER, EAST, HORIZONTAL, LEADING, LEFT, NEXT, NORTH, NORTH_EAST, NORTH_WEST, PREVIOUS, RIGHT, SOUTH, SOUTH_EAST, SOUTH_WEST, TOP, TRAILING, VERTICAL, WESTABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH| コンストラクタと説明 |
|---|
JTextField()
新しい
TextField を構築します。 |
JTextField(Document doc, String text, int columns)
指定されたテキストストレージモデルと列数を使用する新規の
JTextField を構築します。 |
JTextField(int columns)
指定された列数で新規の空の
TextField を構築します。 |
JTextField(String text)
指定されたテキストで初期化された新しい
TextField を構築します。 |
JTextField(String text, int columns)
指定されたテキストおよび列で初期化される新規
TextField を構築します。 |
| 修飾子と型 | メソッドと説明 |
|---|---|
protected void |
actionPropertyChanged(Action action, String propertyName)
関連アクションのプロパティーの変更に応じてテキストフィールドの状態を更新します。
|
void |
addActionListener(ActionListener l)
アクションイベントをこのテキストフィールドから受け取るために、指定されたアクションリスナーを追加します。
|
protected void |
configurePropertiesFromAction(Action a)
指定された
Action のプロパティーに一致するように、このテキストフィールドにプロパティーを設定します。 |
protected PropertyChangeListener |
createActionPropertyChangeListener(Action a)
指定された
Action からの変更を待機し、適切なプロパティーを更新する役割を担う PropertyChangeListener を作成して、返します。 |
protected Document |
createDefaultModel()
モデルが明示的に指定されない場合は、構築時に使うモデルのデフォルト実装を作成します。
|
protected void |
fireActionPerformed()
このイベントタイプの通知対象として登録されているすべてのリスナーに通知します。
|
AccessibleContext |
getAccessibleContext()
この
JTextField に関連付けられている AccessibleContext を取得します。 |
Action |
getAction()
この
ActionEvent ソースに現在設定されている Action を返します。Action が設定されていない場合は、null を返します。 |
ActionListener[] |
getActionListeners()
addActionListener() を使用してこの JTextField に追加されたすべての
ActionListener の配列を返します。 |
Action[] |
getActions()
エディタのコマンドリストを取得します。
|
int |
getColumns()
この
TextField の列数を返します。 |
protected int |
getColumnWidth()
列幅を取得します。
|
int |
getHorizontalAlignment()
テキストの水平配置を返します。
|
BoundedRangeModel |
getHorizontalVisibility()
テキストフィールドの可視性を返します。
|
Dimension |
getPreferredSize()
この
TextField に必要とされる適切なサイズ Dimensions を返します。 |
int |
getScrollOffset()
ピクセル単位のスクロールオフセットを取得します。
|
String |
getUIClassID()
UI のクラス ID を取得します。
|
boolean |
isValidateRoot()
テキストフィールド自身の中から呼び出される
revalidate は、テキストフィールドの妥当性を検証することによって処理されます。ただし、テキストフィールドが JViewport 内に含まれていない場合は false を返します。 |
protected String |
paramString()
この
JTextField の文字列表現を返します。 |
void |
postActionEvent()
このテキストフィールドで発生するアクションイベントを、登録されているすべての
ActionListener オブジェクトにディスパッチすることによって処理します。 |
void |
removeActionListener(ActionListener l)
以後このテキストフィールドからアクションイベントを受け取らないように、指定されたアクションリスナーを削除します。
|
void |
scrollRectToVisible(Rectangle r)
フィールドを左または右にスクロールします。
|
void |
setAction(Action a)
この
ActionEvent ソースの Action を設定します。 |
void |
setActionCommand(String command)
アクションイベントに使うコマンド文字列を設定します。
|
void |
setColumns(int columns)
この
TextField の列数を設定し、配置を無効にします。 |
void |
setDocument(Document doc)
エディタをテキストドキュメントに関連付けます。
|
void |
setFont(Font f)
現在のフォントを設定します。
|
void |
setHorizontalAlignment(int alignment)
テキストの水平配置を設定します。
|
void |
setScrollOffset(int scrollOffset)
ピクセル単位のスクロールオフセットを設定します。
|
addCaretListener, addInputMethodListener, addKeymap, copy, cut, fireCaretUpdate, getCaret, getCaretColor, getCaretListeners, getCaretPosition, getDisabledTextColor, getDocument, getDragEnabled, getDropLocation, getDropMode, getFocusAccelerator, getHighlighter, getInputMethodRequests, getKeymap, getKeymap, getMargin, getNavigationFilter, getPreferredScrollableViewportSize, getPrintable, getScrollableBlockIncrement, getScrollableTracksViewportHeight, getScrollableTracksViewportWidth, getScrollableUnitIncrement, getSelectedText, getSelectedTextColor, getSelectionColor, getSelectionEnd, getSelectionStart, getText, getText, getToolTipText, getUI, isEditable, loadKeymap, modelToView, moveCaretPosition, paste, print, print, print, processInputMethodEvent, read, removeCaretListener, removeKeymap, removeNotify, replaceSelection, restoreComposedText, saveComposedText, select, selectAll, setCaret, setCaretColor, setCaretPosition, setComponentOrientation, setDisabledTextColor, setDragEnabled, setDropMode, setEditable, setFocusAccelerator, setHighlighter, setKeymap, setMargin, setNavigationFilter, setSelectedTextColor, setSelectionColor, setSelectionEnd, setSelectionStart, setText, setUI, updateUI, viewToModel, writeaddAncestorListener, addNotify, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBaseline, getBaselineResizeBehavior, getBorder, getBounds, getClientProperty, getComponentGraphics, getComponentPopupMenu, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getFontMetrics, getGraphics, getHeight, getInheritsPopupMenu, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getListeners, getLocation, getMaximumSize, getMinimumSize, getNextFocusableComponent, getPopupLocation, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getTopLevelAncestor, getTransferHandler, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, hide, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, isPaintingForPrint, isPaintingOrigin, isPaintingTile, isRequestFocusEnabled, paint, paintBorder, paintChildren, paintComponent, paintImmediately, paintImmediately, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processKeyBinding, processKeyEvent, processMouseEvent, processMouseMotionEvent, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, resetKeyboardActions, reshape, revalidate, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setComponentPopupMenu, setDebugGraphicsOptions, setDefaultLocale, setDoubleBuffered, setEnabled, setFocusTraversalKeys, setForeground, setInheritsPopupMenu, setInputMap, setInputVerifier, setMaximumSize, setMinimumSize, setNextFocusableComponent, setOpaque, setPreferredSize, setRequestFocusEnabled, setToolTipText, setTransferHandler, setUI, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, updateadd, 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, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setLayout, transferFocusDownCycle, validate, validateTreeaction, 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, getBackground, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getForeground, getGraphicsConfiguration, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getKeyListeners, getLocale, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPeer, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getToolkit, getTreeLock, gotFocus, handleEvent, hasFocus, 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, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, resize, resize, setBounds, setBounds, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, setLocation, setLocation, setName, setSize, setSize, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCyclepublic JTextField()
TextField を構築します。デフォルトモデルが作成され、初期文字列は null で、列数が 0 に設定されます。public JTextField(String text)
TextField を構築します。デフォルトモデルが作成され、列数は 0 です。text - 表示されるテキストまたは nullpublic JTextField(int columns)
TextField を構築します。デフォルトモデルが作成され、初期文字列は null に設定されます。columns - 適切な幅の計算に使用する列数。列数を 0 に設定すると、適切な幅はコンポーネントの実装から自然に得られる値になるpublic JTextField(String text, int columns)
TextField を構築します。デフォルトモデルが作成されます。text - 表示されるテキストまたは nullcolumns - 適切な幅の計算に使用する列数。列数を 0 に設定すると、適切な幅はコンポーネントの実装から自然に得られる値になるpublic JTextField(Document doc, String text, int columns)
JTextField を構築します。これは、ほかのコンストラクタが入力するコンストラクタです。ドキュメントが null の場合はデフォルトモデルが作成されます。doc - 使用するテキストストレージ。null の場合は、createDefaultModel メソッドを呼び出すことによってデフォルトの値が提供されるtext - 表示する初期文字列または nullcolumns - 適切な幅の計算に使用する列数。0 以上。columns を 0 に設定すると、適切な幅はコンポーネントの実装から自然に得られる値になるIllegalArgumentException - columns が 0 未満の場合public String getUIClassID()
getUIClassID 、クラス: JComponentJComponent.getUIClassID(), UIDefaults.getUI(javax.swing.JComponent)public void setDocument(Document doc)
setDocument 、クラス: JTextComponentdoc - 表示および編集するドキュメントJTextComponent.getDocument()public boolean isValidateRoot()
revalidate は、テキストフィールドの妥当性を検証することによって処理されます。ただし、テキストフィールドが JViewport 内に含まれていない場合は false を返します。isValidateRoot 、クラス: JComponentJViewPort の場合は false、そうでない場合は trueJComponent.revalidate(), JComponent.isValidateRoot(), Container.isValidateRoot()public int getHorizontalAlignment()
JTextField.LEFT
JTextField.CENTER
JTextField.RIGHT
JTextField.LEADING
JTextField.TRAILING
public void setHorizontalAlignment(int alignment)
JTextField.LEFT
JTextField.CENTER
JTextField.RIGHT
JTextField.LEADING
JTextField.TRAILING
invalidate と repaint が呼び出され、PropertyChange イベント (horizontalAlignment) がトリガーされます。alignment - 配置方法IllegalArgumentException - alignment が有効なキーでない場合protected Document createDefaultModel()
PlainDocument のインスタンスが返されます。public int getColumns()
TextField の列数を返します。public void setColumns(int columns)
TextField の列数を設定し、配置を無効にします。columns - 列数 >= 0IllegalArgumentException - columns が 0 より小さい場合protected int getColumnWidth()
public Dimension getPreferredSize()
TextField に必要とされる適切なサイズ Dimensions を返します。ゼロ以外の列数が設定されていると、その幅は列数を列幅でかけた値に設定されます。getPreferredSize 、クラス: JComponentJComponent.setPreferredSize(java.awt.Dimension), ComponentUIpublic void setFont(Font f)
revalidate は、フォント設定後に呼び出されます。setFont 、クラス: JComponentf - 新規フォントComponent.getFont()public void addActionListener(ActionListener l)
l - 追加されるアクションリスナーpublic void removeActionListener(ActionListener l)
l - 削除されるアクションリスナーpublic ActionListener[] getActionListeners()
ActionListener の配列を返します。ActionListener。リスナーが追加されていない場合は空の配列protected void fireActionPerformed()
EventListenerListpublic void setActionCommand(String command)
command - コマンド文字列public void setAction(Action a)
ActionEvent ソースの Action を設定します。新しい Action は設定済みの Action と置き換わりますが、ActionListeners で独立して追加された addActionListener には影響しません。Action がすでに ActionEvent ソースにすでに登録されている ActionListener である場合は、再登録されません。
Action を設定すると、「Action をサポートする Swing コンポーネント」で説明されているすべてのプロパティーがすぐに変更されます。続いて、Action のプロパティーの変更に合わせて、テキストフィールドのプロパティーが自動的に更新されます。
このメソッドは、Action のプロパティー値を設定し、追跡するために、ほかの 3 つのメソッドを使用します。このメソッドは、テキストフィールドのプロパティーをただちに変更するために、configurePropertiesFromAction メソッドを使用します。Action のプロパティー値の変更を追跡するために、このメソッドは createActionPropertyChangeListener から返される PropertyChangeListener を登録します。デフォルトの PropertyChangeListener は、Action のプロパティーに変更があると、actionPropertyChanged メソッドを呼び出します。
a - JTextField の Action、または nullAction, getAction(), configurePropertiesFromAction(javax.swing.Action), createActionPropertyChangeListener(javax.swing.Action), actionPropertyChanged(javax.swing.Action, java.lang.String)public Action getAction()
ActionEvent ソースに現在設定されている Action を返します。Action が設定されていない場合は、null を返します。ActionEvent ソースの Action、または nullAction, setAction(javax.swing.Action)protected void configurePropertiesFromAction(Action a)
Action のプロパティーに一致するように、このテキストフィールドにプロパティーを設定します。これによって設定されるプロパティーの詳細は、「Action をサポートする Swing コンポーネント」を参照してください。a - プロパティーを取得する Action、または nullAction, setAction(javax.swing.Action)protected void actionPropertyChanged(Action action, String propertyName)
createActionPropertyChangeListener から返される PropertyChangeListener から呼び出されます。サブクラスは、通常、これを呼び出す必要はありません。追加の Action プロパティーをサポートするサブクラスは、これと configurePropertiesFromAction をオーバーライドする必要があります。
このメソッドによって設定されるプロパティーのリストは、「Action をサポートする Swing コンポーネント」の表を参照してください。
action - このテキストフィールドに関連付けられた ActionpropertyName - 変更されたプロパティーの名前Action, configurePropertiesFromAction(javax.swing.Action)protected PropertyChangeListener createActionPropertyChangeListener(Action a)
Action からの変更を待機し、適切なプロパティーを更新する役割を担う PropertyChangeListener を作成して、返します。
警告: これをサブクラス化する場合、匿名の内部クラスは作成しないでください。作成すると、テキストフィールドの寿命が Action の寿命に拘束されます。
a - テキストフィールドのアクションAction, setAction(javax.swing.Action)public Action[] getActions()
getActions 、クラス: JTextComponentpublic void postActionEvent()
ActionListener オブジェクトにディスパッチすることによって処理します。これは一般的に、テキストフィールドに登録されたコントローラによって呼び出されます。public BoundedRangeModel getHorizontalVisibility()
フィールドの Look & Feel の実装は、BoundedRangeModel の最小値、最大値、長さの各プロパティーの値を管理します。
BoundedRangeModelpublic int getScrollOffset()
public void setScrollOffset(int scrollOffset)
scrollOffset - オフセット >= 0public void scrollRectToVisible(Rectangle r)
scrollRectToVisible 、クラス: JComponentr - スクロール対象の範囲JViewportprotected String paramString()
JTextField の文字列表現を返します。このメソッドはデバッグ専用であり、返される文字列の内容および形式は実装によって異なります。返される文字列は空の場合がありますが、null にはなりません。paramString 、クラス: JTextComponentJTextField の文字列表現public AccessibleContext getAccessibleContext()
JTextField に関連付けられている AccessibleContext を取得します。JTextFields の場合、AccessibleContext は AccessibleJTextField の形式を取ります。必要に応じて、新しい AccessibleJTextField インスタンスが作成されます。getAccessibleContext 、インタフェース: AccessiblegetAccessibleContext 、クラス: JTextComponentJTextField の AccessibleContext として機能する AccessibleJTextField バグまたは機能を送信
詳細な API リファレンスおよび開発者ドキュメントについては、Java SE のドキュメントを参照してください。そのドキュメントには、概念的な概要、用語の定義、回避方法、有効なコード例などの、開発者を対象にしたより詳細な説明が含まれています。
Copyright © 1993, 2013, Oracle and/or its affiliates. All rights reserved.