public class JFormattedTextField extends JTextField
JFormattedTextField は JTextField を拡張して、任意の値をフォーマットしたり、ユーザーがテキストを編集したあとに特定のオブジェクトを取得したりするためのサポートを追加します。JFormattedTextField の日付編集用の設定例を次に挙げます。
JFormattedTextField ftf = new JFormattedTextField(); ftf.setValue(new Date());
JFormattedTextField が一度生成されると、PropertyChangeListener を追加し、プロパティー名 value を使用して PropertyChangeEvent を待機することで、変更の編集を待機できます。
JFormattedTextField により、フォーカスが失われた場合に実行するアクションの設定が可能になります。次の設定が用意されています。
値 | 説明 |
|---|---|
| JFormattedTextField.REVERT | getValue の表示に合うように、表示を元に戻す。現在の編集は失われる可能性がある。
|
| JFormattedTextField.COMMIT | 現在の値を確定。現在の値が AbstractFormatter による正当な値ではなく、ParseException がスローされる場合、値は変更されず、編集された値がそのまま残る。
|
| JFormattedTextField.COMMIT_OR_REVERT | COMMIT とほぼ同じ。値が正当なものでない場合は REVERT と同様。
|
| JFormattedTextField.PERSIST | 何も処理しない。新しい AbstractFormatter の取得、値の更新、どちらも実行しない。
|
JFormattedTextField.COMMIT_OR_REVERT です。詳細については、setFocusLostBehavior(int) を参照してください。
JFormattedTextField により、現在編集中の値が不正な場合でもフォーカスは移動が可能になります。JFormattedTextField の編集状態が不正な場合にフォーカスをロックするには、InputVerifier を接続します。このような InputVerifier を実装したコードの一部を例として挙げます。
public class FormattedTextFieldVerifier extends InputVerifier {
public boolean verify(JComponent input) {
if (input instanceof JFormattedTextField) {
JFormattedTextField ftf = (JFormattedTextField)input;
AbstractFormatter formatter = ftf.getFormatter();
if (formatter != null) {
String text = ftf.getText();
try {
formatter.stringToValue(text);
return true;
} catch (ParseException pe) {
return false;
}
}
}
return true;
}
public boolean shouldYieldFocus(JComponent input) {
return verify(input);
}
}
commitEdit を呼び出すことでも値を確定できます。
JFormattedTextField はそれ自体ではフォーマットを行わず、JFormattedTextField.AbstractFormatterFactory のインスタンスから取得される、JFormattedTextField.AbstractFormatter のインスタンスでフォーマットを行います。JFormattedTextField.AbstractFormatter のインスタンスは、アクティブになったときに install メソッドから通知を受け、JFormattedTextField.AbstractFormatter はその時点で必要なオブジェクトをインストールできます。通常は DocumentFilter がインストールされます。JFormattedTextField で AbstractFormatter が不要になった場合は、同様に uninstall が呼び出されます。
JFormattedTextField は、通常、フォーカスを取得または喪失したときに、AbstractFormat に対する AbstractFormatterFactory を照会します。ただし、これはフォーカス喪失ポリシーに基づいて変更できます。フォーカス喪失ポリシーが JFormattedTextField.PERSIST であり、JFormattedTextField が編集されている場合、AbstractFormatterFactory は値が確定されるまで照会されません。同様に、フォーカス喪失ポリシーが JFormattedTextField.COMMIT であり、例外が stringToValue からスローされる場合は、AbstractFormatterFactory はフォーカスが喪失または取得されるまで照会されません。
JFormattedTextField.AbstractFormatter は、確定値の JFormattedTextField への設定タイミングも決定します。JFormattedTextField.AbstractFormatter のなかには、編集のたびに新しく値を生成するものもあり、まったく値を確定しないものもあります。commitEdit を呼び出すことで、現在の値を現在の JFormattedTextField.AbstractFormatter から強制的に取得できるようになります。
commitEdit は JFormattedTextField で [return] を押すたびに呼び出されます。
AbstractFormatterFactory が明示的に設定されていない場合で、値が null 以外のときには、setValue が呼び出されたあとに、値の型 Class に基づいてこれが設定されます。たとえば、次のコードでは、適切な AbstractFormatterFactory と AbstractFormatter が、数値のフォーマット処理のために生成されます。
JFormattedTextField tf = new JFormattedTextField(); tf.setValue(new Number(100));
警告: AbstractFormatter は、通常、Document に DocumentFilter をインストールし、JFormattedTextField に NavigationFilter をインストールするため、この 2 つを独自にインストールしないでください。独自にインストールすると、動作が奇妙になり、AbstractFormatter の編集ポリシーが強化されなくなります。
警告: Swing はスレッドに対して安全ではありません。詳細は、「Swing's Threading Policy」を参照してください。
警告: このクラスの直列化されたオブジェクトは、今後の Swing リリースと互換ではなくなる予定です。現在の直列化のサポートは、短期間の格納や、同じバージョンの Swing を実行するアプリケーション間の RMI に適しています。1.4 以降、すべての JavaBeansTM 用の長期間の格納サポートが java.beans パッケージに追加されています。XMLEncoder を参照してください。
| 修飾子と型 | クラスと説明 |
|---|---|
static class |
JFormattedTextField.AbstractFormatter
AbstractFormatter のインスタンスは、Object から String および String から Object への変換を処理するために JFormattedTextField で使用されます。 |
static class |
JFormattedTextField.AbstractFormatterFactory
AbstractFormatterFactory のインスタンスは JFormattedTextField で使用され、値のフォーマットに使用される AbstractFormatter のインスタンスを取得します。 |
JTextField.AccessibleJTextFieldJTextComponent.AccessibleJTextComponent, JTextComponent.DropLocation, JTextComponent.KeyBindingJComponent.AccessibleJComponentContainer.AccessibleAWTContainerComponent.AccessibleAWTComponent, Component.BaselineResizeBehavior, Component.BltBufferStrategy, Component.FlipBufferStrategy| 修飾子と型 | フィールドと説明 |
|---|---|
static int |
COMMIT
フォーカス喪失時に
commitEdit を呼び出すことを指定する定数です。 |
static int |
COMMIT_OR_REVERT
フォーカス喪失時に
commitEdit を呼び出すことを指定する定数です。 |
static int |
PERSIST
フォーカス喪失時に編集されている値を残しておくことを指定する定数です。
|
static int |
REVERT
フォーカス喪失時に、編集中の値を
JFormattedTextField の現在の値セットに戻すことを指定する定数です。 |
notifyActionDEFAULT_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| コンストラクタと説明 |
|---|
JFormattedTextField()
AbstractFormatterFactory を使用しないで JFormattedTextField を生成します。 |
JFormattedTextField(Format format)
JFormattedTextField を作成します。 |
JFormattedTextField(JFormattedTextField.AbstractFormatter formatter)
指定された
AbstractFormatter で JFormattedTextField を作成します。 |
JFormattedTextField(JFormattedTextField.AbstractFormatterFactory factory)
指定された
AbstractFormatterFactory で JFormattedTextField を作成します。 |
JFormattedTextField(JFormattedTextField.AbstractFormatterFactory factory, Object currentValue)
指定された
AbstractFormatterFactory と初期値を使用して JFormattedTextField を生成します。 |
JFormattedTextField(Object value)
指定された値を使用して JFormattedTextField を生成します。
|
| 修飾子と型 | メソッドと説明 |
|---|---|
void |
commitEdit()
現在の値を
AbstractFormatter から強制的に取得し、現在の値として設定します。 |
Action[] |
getActions()
エディタのコマンドリストを取得します。
|
int |
getFocusLostBehavior()
フォーカス喪失時の動作を返します。
|
JFormattedTextField.AbstractFormatter |
getFormatter()
現在の値のフォーマットと構文解析に使用される
AbstractFormatter を返します。 |
JFormattedTextField.AbstractFormatterFactory |
getFormatterFactory()
現在の
AbstractFormatterFactory を返します。 |
String |
getUIClassID()
UI のクラス ID を取得します。
|
Object |
getValue()
最新の有効な値を返します。
|
protected void |
invalidEdit()
ユーザーが無効な値を入力した場合に呼び出されます。
|
boolean |
isEditValid()
現在編集中の値が有効な場合に true を返します。
|
protected void |
processFocusEvent(FocusEvent e)
FocusEvent.FOCUS_GAINED や FocusEvent.FOCUS_LOST などのフォーカスイベントを処理します。 |
protected void |
processInputMethodEvent(InputMethodEvent e)
InputMethodEvent.INPUT_METHOD_TEXT_CHANGED や InputMethodEvent.CARET_POSITION_CHANGED などのインプットメソッドイベントを処理します。 |
void |
setDocument(Document doc)
エディタをテキストドキュメントに関連付けます。
|
void |
setFocusLostBehavior(int behavior)
フォーカス喪失時の動作を設定します。
|
protected void |
setFormatter(JFormattedTextField.AbstractFormatter format)
現在の
AbstractFormatter を設定します。 |
void |
setFormatterFactory(JFormattedTextField.AbstractFormatterFactory tf)
AbstractFormatterFactory を設定します。 |
void |
setValue(Object value)
現在の
AbstractFormatterFactory から取得した AbstractFormatter でフォーマットされる値を設定します。 |
actionPropertyChanged, addActionListener, configurePropertiesFromAction, createActionPropertyChangeListener, createDefaultModel, fireActionPerformed, getAccessibleContext, getAction, getActionListeners, getColumns, getColumnWidth, getHorizontalAlignment, getHorizontalVisibility, getPreferredSize, getScrollOffset, isValidateRoot, paramString, postActionEvent, removeActionListener, scrollRectToVisible, setAction, setActionCommand, setColumns, setFont, setHorizontalAlignment, setScrollOffsetaddCaretListener, 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, 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, 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 static final int COMMIT
commitEdit を呼び出すことを指定する定数です。新しい値の確定時に ParseException がスローされると、無効な値がそのまま残ることになります。public static final int COMMIT_OR_REVERT
commitEdit を呼び出すことを指定する定数です。新しい値の確定時に ParseException がスローされると、元の値に戻されます。public static final int REVERT
JFormattedTextField の現在の値セットに戻すことを指定する定数です。public static final int PERSIST
public JFormattedTextField()
AbstractFormatterFactory を使用しないで JFormattedTextField を生成します。特定の型の値を編集するように JFormattedTextField を設定する場合は、setMask または setFormatterFactory を使用してください。public JFormattedTextField(Object value)
value の型に基づいた AbstractFormatterFactory が生成されます。value - JFormattedTextField の初期値public JFormattedTextField(Format format)
JFormattedTextField を作成します。format は適切な AbstractFormatter にラップされ、AbstractFormatter は AbstractFormatterFactory にラップされます。format - AbstractFormatter の検索に使用されるフォーマットpublic JFormattedTextField(JFormattedTextField.AbstractFormatter formatter)
AbstractFormatter で JFormattedTextField を作成します。AbstractFormatter は AbstractFormatterFactory に配置されます。formatter - フォーマットで使用される AbstractFormatter。public JFormattedTextField(JFormattedTextField.AbstractFormatterFactory factory)
AbstractFormatterFactory で JFormattedTextField を作成します。factory - フォーマットに使用される AbstractFormatterFactory。public JFormattedTextField(JFormattedTextField.AbstractFormatterFactory factory, Object currentValue)
AbstractFormatterFactory と初期値を使用して JFormattedTextField を生成します。factory - フォーマットに使用される AbstractFormatterFactory。currentValue - 使用される初期値public void setFocusLostBehavior(int behavior)
JFormattedTextField.COMMIT_OR_REVERT、JFormattedTextField.REVERT、JFormattedTextField.COMMIT、または JFormattedTextField.PERSIST のいずれかです。AbstractFormatter の中には、この値により影響を与えないように、変更発生時に変更をプッシュするものがあります。
渡されるオブジェクトの値が前述の値でない場合は IllegalArgumentException がスローされます。
このプロパティーのデフォルト値は JFormattedTextField.COMMIT_OR_REVERT です。
behavior - フォーカス喪失時の動作IllegalArgumentException - 動作が指定された値で示されるものでない場合public int getFocusLostBehavior()
COMMIT_OR_REVERT、COMMIT、REVERT、または PERSIST のいずれかです。AbstractFormatter の中には、この値により影響を与えないように、変更発生時に変更をプッシュするものがあります。public void setFormatterFactory(JFormattedTextField.AbstractFormatterFactory tf)
AbstractFormatterFactory を設定します。
AbstractFormatterFactory は、表示値のフォーマット、および編集ポリシー強化に使用される AbstractFormatter のインスタンスを返すことができます。
このメソッドまたはコンストラクタで AbstractFormatterFactory が明示的に設定されていない場合、AbstractFormatterFactory、そして結果的に AbstractFormatter は値の Class に基づいて使用されます。
Number には NumberFormatter、Dates には DateFormatter、それ以外には DefaultFormatter が使用されます。
これは JavaBeans バウンドプロパティーです。
tf - AbstractFormatter のインスタンス検索に使用される AbstractFormatterFactorypublic JFormattedTextField.AbstractFormatterFactory getFormatterFactory()
AbstractFormatterFactory を返します。AbstractFormatter の特定に使用される AbstractFormatterFactorysetFormatterFactory(javax.swing.JFormattedTextField.AbstractFormatterFactory)protected void setFormatter(JFormattedTextField.AbstractFormatter format)
AbstractFormatter を設定します。
通常、これを呼び出す代わりに、AbstractFormatterFactory またはその値を設定します。
JFormattedTextField は、これを JFormattedTextField 変更の状態として呼び出し、値のリセットを要求します。JFormattedTextField は AbstractFormatterFactory から取得した AbstractFormatter を渡します。
これは JavaBeans バウンドプロパティーです。
format - フォーマットで使用される AbstractFormattersetFormatterFactory(javax.swing.JFormattedTextField.AbstractFormatterFactory)public JFormattedTextField.AbstractFormatter getFormatter()
AbstractFormatter を返します。public void setValue(Object value)
AbstractFormatterFactory から取得した AbstractFormatter でフォーマットされる値を設定します。AbstractFormatterFactory が指定されていない場合は、value の型に基づいてその生成を試みます。
このプロパティーのデフォルト値は null です。
これは JavaBeans バウンドプロパティーです。
value - 表示される現在の値public Object getValue()
AbstractFormatter の編集ポリシーに基づいて、現在の値が返されない場合があります。現在編集されている値は getValue のあと commitEdit を呼び出すことによって取得できます。public void commitEdit()
throws ParseException
AbstractFormatter から強制的に取得し、現在の値として設定します。AbstractFormatter が現在インストールされていない場合は何も実行しません。ParseException - AbstractFormatter が現在の値をフォーマットできない場合public boolean isEditValid()
AbstractFormatter で管理されます。この値を public に設定する機能はありません。protected void invalidEdit()
protected void processInputMethodEvent(InputMethodEvent e)
InputMethodEvent.INPUT_METHOD_TEXT_CHANGED や InputMethodEvent.CARET_POSITION_CHANGED などのインプットメソッドイベントを処理します。processInputMethodEvent 、クラス: JTextComponente - InputMethodEventInputMethodEventprotected void processFocusEvent(FocusEvent e)
FocusEvent.FOCUS_GAINED や FocusEvent.FOCUS_LOST などのフォーカスイベントを処理します。processFocusEvent 、クラス: Componente - FocusEventFocusEventpublic Action[] getActions()
getActions 、クラス: JTextFieldpublic String getUIClassID()
getUIClassID 、クラス: JTextFieldJComponent.getUIClassID()public void setDocument(Document doc)
setDocument 、クラス: JTextFielddoc - 表示および編集するドキュメントJTextComponent.getDocument() バグまたは機能を送信
詳細な API リファレンスおよび開発者ドキュメントについては、Java SE のドキュメントを参照してください。そのドキュメントには、概念的な概要、用語の定義、回避方法、有効なコード例などの、開発者を対象にしたより詳細な説明が含まれています。
Copyright © 1993, 2013, Oracle and/or its affiliates. All rights reserved.