Class NumberEditorExt

  • All Implemented Interfaces:
    java.io.Serializable, javax.swing.CellEditor, javax.swing.table.TableCellEditor, javax.swing.tree.TreeCellEditor

    public class NumberEditorExt
    extends javax.swing.DefaultCellEditor
    Issue #393-swingx: localized NumberEditor. Added feature to use StrictNumberFormatter.
    See Also:
    Serialized Form
    • Nested Class Summary

      • Nested classes/interfaces inherited from class javax.swing.DefaultCellEditor

        javax.swing.DefaultCellEditor.EditorDelegate
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static java.lang.Class<?>[] argTypes  
      (package private) java.lang.reflect.Constructor<?> constructor  
      private boolean useStrictFormatter  
      • Fields inherited from class javax.swing.DefaultCellEditor

        clickCountToStart, delegate, editorComponent
      • Fields inherited from class javax.swing.AbstractCellEditor

        changeEvent, listenerList
    • Constructor Summary

      Constructors 
      Constructor Description
      NumberEditorExt()
      Instantiates an editor with default NumberFormat and default NumberFormatter.
      NumberEditorExt​(boolean useStrictFormatter)
      Instantiates an editor with default NumberFormat and NumberFormatter depending on useStrictFormatter.
      NumberEditorExt​(java.text.NumberFormat format)
      Instantiates an editor with the given NumberFormat and default NumberFormatter.
      NumberEditorExt​(java.text.NumberFormat format, boolean useStrictFormatter)
      Instantiates an editor with the given NumberFormat and NumberFormatter depending on useStrictFormatter.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      private static javax.swing.JFormattedTextField createFormattedTextField​(java.text.NumberFormat formatter)
      Creates and returns a JFormattedTextField configured with defaults.
      private static javax.swing.JFormattedTextField createFormattedTextFieldX​(java.text.NumberFormat format)
      Creates and returns a JFormattedTextField configured with SwingX extended NumberFormat and StrictNumberFormatter.
      java.lang.Number getCellEditorValue()
      javax.swing.JFormattedTextField getComponent()
      protected java.lang.Number getNumber()
      Returns the editor value as number.
      java.awt.Component getTableCellEditorComponent​(javax.swing.JTable table, java.lang.Object value, boolean isSelected, int row, int column)
      Override and set the border back to normal in case there was an error previously
      protected boolean hasStrictFormatter()  
      protected boolean isValid()
      Returns a boolean indicating whether the current text is valid for instantiating the expected Number type.
      boolean stopCellEditing()  
      • Methods inherited from class javax.swing.DefaultCellEditor

        cancelCellEditing, getClickCountToStart, getTreeCellEditorComponent, isCellEditable, setClickCountToStart, shouldSelectCell
      • Methods inherited from class javax.swing.AbstractCellEditor

        addCellEditorListener, fireEditingCanceled, fireEditingStopped, getCellEditorListeners, removeCellEditorListener
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
      • Methods inherited from interface javax.swing.CellEditor

        addCellEditorListener, removeCellEditorListener
    • Field Detail

      • argTypes

        private static java.lang.Class<?>[] argTypes
      • constructor

        java.lang.reflect.Constructor<?> constructor
      • useStrictFormatter

        private boolean useStrictFormatter
    • Constructor Detail

      • NumberEditorExt

        public NumberEditorExt()
        Instantiates an editor with default NumberFormat and default NumberFormatter.
      • NumberEditorExt

        public NumberEditorExt​(java.text.NumberFormat format)
        Instantiates an editor with the given NumberFormat and default NumberFormatter.
        Parameters:
        format - the NumberFormat to use for conversion, may be null to indicate usage of default NumberFormat.
      • NumberEditorExt

        public NumberEditorExt​(boolean useStrictFormatter)
        Instantiates an editor with default NumberFormat and NumberFormatter depending on useStrictFormatter.
        Parameters:
        useStrictFormatter - if true, uses a StrictNumberFormatter, else uses default NumberFormatter
      • NumberEditorExt

        public NumberEditorExt​(java.text.NumberFormat format,
                               boolean useStrictFormatter)
        Instantiates an editor with the given NumberFormat and NumberFormatter depending on useStrictFormatter.
        Parameters:
        format - the NumberFormat to use for conversion, may be null to indicate usage of default NumberFormat
        useStrictFormatter - if true, uses a StrictNumberFormatter, else uses default NumberFormatter
    • Method Detail

      • stopCellEditing

        public boolean stopCellEditing()
        Specified by:
        stopCellEditing in interface javax.swing.CellEditor
        Overrides:
        stopCellEditing in class javax.swing.DefaultCellEditor
      • isValid

        protected boolean isValid()
        Returns a boolean indicating whether the current text is valid for instantiating the expected Number type.
        Returns:
        true if text is valid, false otherwise.
      • getNumber

        protected java.lang.Number getNumber()
                                      throws java.lang.Exception
        Returns the editor value as number. May fail for a variety of reasons, as it forces parsing of the current text as well as reflective construction of the target type.
        Returns:
        the editor value or null
        Throws:
        java.lang.Exception - if creation of the expected type fails in some way.
      • hasStrictFormatter

        protected boolean hasStrictFormatter()
        Returns:
      • getTableCellEditorComponent

        public java.awt.Component getTableCellEditorComponent​(javax.swing.JTable table,
                                                              java.lang.Object value,
                                                              boolean isSelected,
                                                              int row,
                                                              int column)
        Override and set the border back to normal in case there was an error previously
        Specified by:
        getTableCellEditorComponent in interface javax.swing.table.TableCellEditor
        Overrides:
        getTableCellEditorComponent in class javax.swing.DefaultCellEditor
      • getCellEditorValue

        public java.lang.Number getCellEditorValue()
                                            throws java.lang.IllegalStateException

        Overridden to instantiate a Number of the expected type. Note that this may throw a IllegalStateException if invoked without querying for a valid value with stopCellEditing. This should not happen during normal usage.

        Specified by:
        getCellEditorValue in interface javax.swing.CellEditor
        Overrides:
        getCellEditorValue in class javax.swing.DefaultCellEditor
        Throws:
        java.lang.IllegalStateException - if current value invalid
      • getComponent

        public javax.swing.JFormattedTextField getComponent()

        Convenience override with type cast.

        Overrides:
        getComponent in class javax.swing.DefaultCellEditor
      • createFormattedTextFieldX

        private static javax.swing.JFormattedTextField createFormattedTextFieldX​(java.text.NumberFormat format)
        Creates and returns a JFormattedTextField configured with SwingX extended NumberFormat and StrictNumberFormatter. This method is called if the constructor parameter useStrictFormatter is true. Use a static method so that we can do some stuff before calling the superclass.
      • createFormattedTextField

        private static javax.swing.JFormattedTextField createFormattedTextField​(java.text.NumberFormat formatter)
        Creates and returns a JFormattedTextField configured with defaults. This method is called if the contructor useStrictFormatter is false.

        Use a static method so that we can do some stuff before calling the superclass.