org.opensha.commons.param.editor.impl
Class NumericTextField

java.lang.Object
  extended by java.awt.Component
      extended by java.awt.Container
          extended by javax.swing.JComponent
              extended by javax.swing.text.JTextComponent
                  extended by javax.swing.JTextField
                      extended by org.opensha.commons.param.editor.impl.NumericTextField
All Implemented Interfaces:
java.awt.image.ImageObserver, java.awt.MenuContainer, java.io.Serializable, javax.accessibility.Accessible, javax.swing.Scrollable, javax.swing.SwingConstants, NumericPlainDocument.InsertErrorListener

public class NumericTextField
extends javax.swing.JTextField
implements NumericPlainDocument.InsertErrorListener

Title: NumericTextField

Description: Special JTextField that only allows numbers to be typed in. This text field allows for normal number synatx, such as a negative sign, period for decimal point, and commas for deliminating thousands, millions, etc.

Note: This is a fairly complex GUI customization that relies upon a NumjericDocument model to determine what types of characters are allowed for a numerical number ( digits, - sign in first location, etc. ) It is beyond the scope of this javadoc to explain it's use fully. It is not necessary for programmers to understand the details. They can just use it like a normal JTextField and just expect it to work.

Please consult Swing doucmentation for further details, specifically JTextField and PlainDocument. It is required a programmer understands the Model View Component design architeture to understand the relationship between JTextField and PlainDocument ( and our corresponding NumericTextField and NumericDocument ).

Version:
1.0
Author:
Steven W. Rock
See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from class javax.swing.JTextField
javax.swing.JTextField.AccessibleJTextField
 
Nested classes/interfaces inherited from class javax.swing.text.JTextComponent
javax.swing.text.JTextComponent.AccessibleJTextComponent, javax.swing.text.JTextComponent.DropLocation, javax.swing.text.JTextComponent.KeyBinding
 
Nested classes/interfaces inherited from class javax.swing.JComponent
javax.swing.JComponent.AccessibleJComponent
 
Nested classes/interfaces inherited from class java.awt.Container
java.awt.Container.AccessibleAWTContainer
 
Nested classes/interfaces inherited from class java.awt.Component
java.awt.Component.AccessibleAWTComponent, java.awt.Component.BaselineResizeBehavior, java.awt.Component.BltBufferStrategy, java.awt.Component.FlipBufferStrategy
 
Field Summary
protected static java.lang.String C
          Class name for debugging.
protected static boolean D
          If true print out debug statements.
protected static java.lang.String S
           
protected static java.lang.String S2
           
 
Fields inherited from class javax.swing.JTextField
notifyAction
 
Fields inherited from class javax.swing.text.JTextComponent
DEFAULT_KEYMAP, FOCUS_ACCELERATOR_KEY
 
Fields inherited from class javax.swing.JComponent
accessibleContext, listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW
 
Fields inherited from class java.awt.Component
BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
 
Fields 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
 
Fields inherited from interface java.awt.image.ImageObserver
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
 
Constructor Summary
NumericTextField()
           
NumericTextField(int columns, java.text.DecimalFormat format)
           
NumericTextField(java.lang.String text)
           
NumericTextField(java.lang.String text, int columns)
           
NumericTextField(java.lang.String text, int columns, java.text.DecimalFormat format)
           
 
Method Summary
protected  javax.swing.text.Document createDefaultModel()
          Function that must be overidded to return the PlainDocument subclass
 void formatChanged()
           
 java.lang.Double getDoubleValue()
           
 java.text.DecimalFormat getFormat()
           
 java.lang.Long getLongValue()
           
 java.lang.Number getNumberValue()
           
 java.lang.String getText()
          Returns the text contained in this TextComponent.
 void insertFailed(NumericPlainDocument doc, int offset, java.lang.String str, javax.swing.text.AttributeSet a)
           
static void main(java.lang.String[] args)
          Tester main function that shows how to use this class.
 void normalize()
           
 void setFormat(java.text.DecimalFormat format)
           
 void setText(java.lang.String t)
          Sets the text of this TextComponent to the specified text.
 void setValue(double d)
           
 void setValue(long l)
           
 void setValue(java.lang.Number number)
           
 
Methods inherited from class javax.swing.JTextField
actionPropertyChanged, addActionListener, configurePropertiesFromAction, createActionPropertyChangeListener, fireActionPerformed, getAccessibleContext, getAction, getActionListeners, getActions, getColumns, getColumnWidth, getHorizontalAlignment, getHorizontalVisibility, getPreferredSize, getScrollOffset, getUIClassID, isValidateRoot, paramString, postActionEvent, removeActionListener, scrollRectToVisible, setAction, setActionCommand, setColumns, setDocument, setFont, setHorizontalAlignment, setScrollOffset
 
Methods 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, getText, getToolTipText, getUI, isEditable, loadKeymap, modelToView, moveCaretPosition, paste, print, print, print, processInputMethodEvent, read, removeCaretListener, removeKeymap, removeNotify, replaceSelection, select, selectAll, setCaret, setCaretColor, setCaretPosition, setComponentOrientation, setDisabledTextColor, setDragEnabled, setDropMode, setEditable, setFocusAccelerator, setHighlighter, setKeymap, setMargin, setNavigationFilter, setSelectedTextColor, setSelectionColor, setSelectionEnd, setSelectionStart, setUI, updateUI, viewToModel, write
 
Methods 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, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, isPaintingForPrint, 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, update
 
Methods 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, transferFocusBackward, transferFocusDownCycle, validate, validateTree
 
Methods 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, getPeer, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getToolkit, getTreeLock, gotFocus, handleEvent, hasFocus, hide, 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, transferFocusUpCycle
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

C

protected static final java.lang.String C
Class name for debugging.

See Also:
Constant Field Values

D

protected static final boolean D
If true print out debug statements.

See Also:
Constant Field Values

S

protected static final java.lang.String S
See Also:
Constant Field Values

S2

protected static final java.lang.String S2
See Also:
Constant Field Values
Constructor Detail

NumericTextField

public NumericTextField()

NumericTextField

public NumericTextField(java.lang.String text,
                        int columns,
                        java.text.DecimalFormat format)

NumericTextField

public NumericTextField(int columns,
                        java.text.DecimalFormat format)

NumericTextField

public NumericTextField(java.lang.String text)

NumericTextField

public NumericTextField(java.lang.String text,
                        int columns)
Method Detail

setFormat

public void setFormat(java.text.DecimalFormat format)

getFormat

public java.text.DecimalFormat getFormat()

formatChanged

public void formatChanged()

getLongValue

public java.lang.Long getLongValue()
                            throws java.text.ParseException
Throws:
java.text.ParseException

getDoubleValue

public java.lang.Double getDoubleValue()
                                throws java.text.ParseException
Throws:
java.text.ParseException

getNumberValue

public java.lang.Number getNumberValue()
                                throws java.text.ParseException
Throws:
java.text.ParseException

setValue

public void setValue(java.lang.Number number)

setValue

public void setValue(long l)

setValue

public void setValue(double d)

normalize

public void normalize()
               throws java.text.ParseException
Throws:
java.text.ParseException

setText

public void setText(java.lang.String t)
Sets the text of this TextComponent to the specified text. If the text is null or empty, has the effect of simply deleting the old text. When text has been inserted, the resulting caret location is determined by the implementation of the caret class.

This method is thread safe, although most Swing methods are not. Please see Threads and Swing for more information.

Note: This is where the text field Document model is consulted to see if the text is viable for insertion.

Overrides:
setText in class javax.swing.text.JTextComponent
Parameters:
t - the new text to be set
See Also:
getText(), DefaultCaret

getText

public java.lang.String getText()
Returns the text contained in this TextComponent. If the underlying document is null, will give a NullPointerException.

Overrides:
getText in class javax.swing.text.JTextComponent
Returns:
the text
Throws:
java.lang.NullPointerException - if the document is null
See Also:
setText(java.lang.String)

insertFailed

public void insertFailed(NumericPlainDocument doc,
                         int offset,
                         java.lang.String str,
                         javax.swing.text.AttributeSet a)
Specified by:
insertFailed in interface NumericPlainDocument.InsertErrorListener

createDefaultModel

protected javax.swing.text.Document createDefaultModel()
Function that must be overidded to return the PlainDocument subclass

Overrides:
createDefaultModel in class javax.swing.JTextField

main

public static void main(java.lang.String[] args)
Tester main function that shows how to use this class.