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で[Enter]を押すたびに呼び出されます。
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以降、すべてのJavaBeans(tm)用の長期間の格納サポートが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_KEYlistenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOWaccessibleContext, BOTTOM_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, 2014, Oracle and/or its affiliates. All rights reserved.