- Type Parameters:
T- The type for parsing, validation, and re-formatting.
- All Implemented Interfaces:
ImageObserver,MenuContainer,Serializable,Accessible,Scrollable,SwingConstants
- Direct Known Subclasses:
AoTextField.Integer,AoTextField.String,AoTextField.TrimmedString
JTextField with the following features.
- Automatic selection when gains focus.
- Parsing to the given type.
- Registrable value validators once parsing completes.
- Re-formatting once parsing successful and all validators have passed.
TODO: Should we extend JFormattedTextField instead?
Are we duplicating too much of what it already provides?
- Author:
- AO Industries, Inc.
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceFormats the type-specific value to text, only called for values that pass all registered validators.static classA text field containing anIntegervalue.static interfaceParses the non-null text into the type-specific value orOptional.empty()if field is empty or effectively empty (such as white-space only depending on parser).static classA text field containing aStringvalue without any trimming.static classA text field containing a trimmedString.static interfaceValidates a non-null value.Nested classes/interfaces inherited from class javax.swing.JTextField
JTextField.AccessibleJTextFieldNested classes/interfaces inherited from class javax.swing.text.JTextComponent
JTextComponent.AccessibleJTextComponent, JTextComponent.DropLocation, JTextComponent.KeyBindingNested classes/interfaces inherited from class javax.swing.JComponent
JComponent.AccessibleJComponentNested classes/interfaces inherited from class java.awt.Container
Container.AccessibleAWTContainerNested classes/interfaces inherited from class java.awt.Component
Component.AccessibleAWTComponent, Component.BaselineResizeBehavior, Component.BltBufferStrategy, Component.FlipBufferStrategy -
Field Summary
Fields inherited from class javax.swing.JTextField
notifyActionFields inherited from class javax.swing.text.JTextComponent
DEFAULT_KEYMAP, FOCUS_ACCELERATOR_KEYFields inherited from class javax.swing.JComponent
listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOWFields inherited from class java.awt.Component
accessibleContext, BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENTFields inherited from interface java.awt.image.ImageObserver
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTHFields 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 -
Constructor Summary
ConstructorsConstructorDescriptionAoTextField(AoTextField.Parser<? extends T> parser, AoTextField.Formatter<? super T> formatter) AoTextField(AoTextField.Parser<? extends T> parser, AoTextField.Formatter<? super T> formatter, int columns) AoTextField(AoTextField.Parser<? extends T> parser, AoTextField.Formatter<? super T> formatter, Document doc, T value, int columns) AoTextField(AoTextField.Parser<? extends T> parser, AoTextField.Formatter<? super T> formatter, T value) AoTextField(AoTextField.Parser<? extends T> parser, AoTextField.Formatter<? super T> formatter, T value, int columns) -
Method Summary
Modifier and TypeMethodDescriptionvoidaddValidator(AoTextField.Validator<? super T> validator) Adds aAoTextField.Validator.getText()Deprecated.getText(int offs, int len) Deprecated.Should not be used directly, usegetValue()instead.getValue()Parses the text into the type-specific value orOptional.empty()if field is empty or effectively empty (such as white-space only depending on parser).voidremoveValidator(AoTextField.Validator<? super T> validator) Removes aAoTextField.Validator, matching by object identity (not by equals).voidRemoves the value of the field.voidRevalidates the current field contents versus the parser and all registered validators.voidDeprecated.Should not be used directly, usesetValue(java.lang.Object)orremoveValue()instead.voidFormats and sets the textual value of the field.Methods inherited from class javax.swing.JTextField
actionPropertyChanged, addActionListener, configurePropertiesFromAction, createActionPropertyChangeListener, createDefaultModel, fireActionPerformed, getAccessibleContext, getAction, getActionListeners, getActions, getColumns, getColumnWidth, getHorizontalAlignment, getHorizontalVisibility, getPreferredSize, getScrollOffset, getUIClassID, isValidateRoot, paramString, postActionEvent, removeActionListener, scrollRectToVisible, setAction, setActionCommand, setColumns, setDocument, setFont, setHorizontalAlignment, setScrollOffsetMethods inherited from class javax.swing.text.JTextComponent
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, getToolTipText, getUI, isEditable, loadKeymap, modelToView, modelToView2D, 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, setUI, updateUI, viewToModel, viewToModel2D, writeMethods inherited from class javax.swing.JComponent
addAncestorListener, 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, updateMethods 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, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setLayout, transferFocusDownCycle, validate, validateTreeMethods 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, 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, 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, requestFocus, requestFocus, requestFocusInWindow, resize, resize, setBounds, setBounds, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, setLocation, setLocation, setMixingCutoutShape, setName, setSize, setSize, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycle
-
Constructor Details
-
AoTextField
public AoTextField(AoTextField.Parser<? extends T> parser, AoTextField.Formatter<? super T> formatter) -
AoTextField
public AoTextField(AoTextField.Parser<? extends T> parser, AoTextField.Formatter<? super T> formatter, T value) -
AoTextField
public AoTextField(AoTextField.Parser<? extends T> parser, AoTextField.Formatter<? super T> formatter, int columns) -
AoTextField
public AoTextField(AoTextField.Parser<? extends T> parser, AoTextField.Formatter<? super T> formatter, T value, int columns) -
AoTextField
public AoTextField(AoTextField.Parser<? extends T> parser, AoTextField.Formatter<? super T> formatter, Document doc, T value, int columns)
-
-
Method Details
-
revalidateValue
public void revalidateValue()Revalidates the current field contents versus the parser and all registered validators. Updates the text color to either default for parseable and valid or to red for invalid.In the case of validators that are effected by external state, such as the value of other fields, this may be called when the other field is updated to ensure consistent validation state.
-
addValidator
Adds aAoTextField.Validator.- See Also:
-
removeValidator
Removes aAoTextField.Validator, matching by object identity (not by equals).- See Also:
-
getText
Deprecated.Should not be used directly, usegetValue()instead.- Overrides:
getTextin classJTextComponent
-
getText
Deprecated.Should not be used directly, usegetValue()instead.- Overrides:
getTextin classJTextComponent- Throws:
BadLocationException
-
getValue
Parses the text into the type-specific value orOptional.empty()if field is empty or effectively empty (such as white-space only depending on parser).If has a value, validates against each registered
AoTextField.Validator.- Returns:
- The valid value, if present.
- Throws:
ParseExceptionValidationException- See Also:
-
setText
Deprecated.Should not be used directly, usesetValue(java.lang.Object)orremoveValue()instead.- Overrides:
setTextin classJTextComponent
-
setValue
Formats and sets the textual value of the field.If value is non-null, validates against each registered
AoTextField.Validator.Only calls
setText(java.lang.String)when value changed to minimize firing of events.When setting to a
nullvalue, may useremoveValue()that does not throwValidationException.- Parameters:
value- The new value, which may benull.- Throws:
ValidationException- See Also:
-
removeValue
public void removeValue()Removes the value of the field.Only calls
setText(java.lang.String)when value changed to minimize firing of events.- See Also:
-

getValue()instead.