Class IntegerTextField


  • public class IntegerTextField
    extends java.lang.Object
    TextField for entering integer numbers, either in decimal or hex.

    This field does continuous checking, so you can't enter a bad value.

    Internally, values are maintained using BigIntegers so this field can contain numbers as large as desired. There are convenience methods for getting the value as either an int or long. If using these convenience methods, you should also set the max allowed value so that users can't enter a value larger than can be represented by the getIntValue() or getLongValue() methods as appropriate.

    There are several configuration options as follows:

    • Allows negative numbers - either support all integer numbers or just non-negative numbers. See setAllowNegativeValues(boolean)
    • Allows hex prefix - If this mode is on, then hex mode is turned on and off automatically depending whether or not the text starts with 0x. Otherwise, the hex/decimal mode is set externally (either programmatically or pressing <CTRL> M) and the user is restricted to the numbers/letters appropriate for that mode. See setAllowsHexPrefix(boolean)
    • Have a max value - a max value can be set (must be positive) such that the user can not type a number greater than the max. Otherwise, the number is unlimited. See setMaxValue(BigInteger)
    • Show the number mode as hint text - If on either "Hex" or "Dec" is displayed lightly in the bottom right portion of the text field. See setShowNumberMode(boolean)
    • Constructor Summary

      Constructors 
      Constructor Description
      IntegerTextField()
      Creates a new IntegerTextField with 5 columns and no initial value
      IntegerTextField​(int columns)
      Creates a new IntegerTextField with the specified number of columns and no initial value
      IntegerTextField​(int columns, long initialValue)
      Creates a new IntegerTextField with the specified number of columns and an initial value
      IntegerTextField​(int columns, java.math.BigInteger initialValue)
      Creates a new IntegerTextField with the specified number of columns and initial value
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void addActionListener​(java.awt.event.ActionListener listener)
      Adds an ActionListener to the TextField.
      void addChangeListener​(javax.swing.event.ChangeListener listener)
      Adds a change listener that will be notified whenever the value changes.
      javax.swing.JComponent getComponent()
      Returns the JTextField component that this class manages.
      int getIntValue()
      Returns the current value as an int.
      long getLongValue()
      Returns the current value as a long.
      java.math.BigInteger getMaxValue()
      Returns the current maximum allowed value.
      java.lang.String getText()
      Returns the current text displayed in the field.
      java.math.BigInteger getValue()
      Returns the current value of the field or null if the field has no current value.
      boolean isHexMode()
      Returns true if in hex mode, false if in decimal mode.
      void removeActionListener​(java.awt.event.ActionListener listener)
      Removes an ActionListener from the TextField.
      void removeChangeListener​(javax.swing.event.ChangeListener listener)
      Removes the changes listener.
      void requestFocus()
      Requests focus to the JTextField
      void selectAll()
      Selects the text in the JTextField
      void setAllowNegativeValues​(boolean b)
      Sets whether or not negative numbers are accepted.
      void setAllowsHexPrefix​(boolean allowsHexPrefix)
      Sets whether on not the field supports the 0x prefix.
      void setDecimalMode()
      Sets the mode to Decimal.
      void setEditable​(boolean editable)
      Sets the editable mode for the JTextField component
      void setEnabled​(boolean enabled)
      Sets the enablement on the JTextField component;
      void setHexMode()
      Sets the radix mode to Hex.
      void setMaxValue​(java.math.BigInteger maxValue)
      Sets the maximum allowed value.
      void setShowNumberMode​(boolean show)
      Turns on or off the faded text that displays the field's radix mode (hex or decimal).
      void setValue​(int newValue)
      Convenience method for setting the value to an int value;
      void setValue​(long newValue)
      Convenience method for setting the value to a long value;
      void setValue​(java.math.BigInteger newValue)
      Sets the value of the field to the given value.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • IntegerTextField

        public IntegerTextField()
        Creates a new IntegerTextField with 5 columns and no initial value
      • IntegerTextField

        public IntegerTextField​(int columns)
        Creates a new IntegerTextField with the specified number of columns and no initial value
        Parameters:
        columns - the number of columns.
      • IntegerTextField

        public IntegerTextField​(int columns,
                                long initialValue)
        Creates a new IntegerTextField with the specified number of columns and an initial value
        Parameters:
        columns - the number of columns to display in the JTextField.
        initialValue - the initial value. This constructor takes an initialValue as a long. If you need a value that is bigger (or smaller) than can be specified as a long, then use the constructor that takes a BigInteger as an initial value.
      • IntegerTextField

        public IntegerTextField​(int columns,
                                java.math.BigInteger initialValue)
        Creates a new IntegerTextField with the specified number of columns and initial value
        Parameters:
        columns - the number of columns
        initialValue - the initial value
    • Method Detail

      • addChangeListener

        public void addChangeListener​(javax.swing.event.ChangeListener listener)
        Adds a change listener that will be notified whenever the value changes.
        Parameters:
        listener - the change listener to add.
      • removeChangeListener

        public void removeChangeListener​(javax.swing.event.ChangeListener listener)
        Removes the changes listener.
        Parameters:
        listener - the listener to be removed.
      • getValue

        public java.math.BigInteger getValue()
        Returns the current value of the field or null if the field has no current value.
        Returns:
        the current value of the field or null if the field has no current value.
      • getIntValue

        public int getIntValue()
        Returns the current value as an int.

        If the field has no current value, 0 will be returned. If the value is bigger (or smaller) than an int, it will be cast to an int.

        If using this method, it is highly recommended that you set the max value to Integer.MAX_VALUE or lower.

        Returns:
        the current value as an int. Or 0 if there is no value
        Throws:
        java.lang.ArithmeticException - if the value in this field will not fit into an int
      • getLongValue

        public long getLongValue()
        Returns the current value as a long.

        If the field has no current value, 0 will be returned. If the value is bigger (or smaller) than an long, it will be cast to a long.

        If using this method, it is highly recommended that you set the max value to Long.MAX_VALUE or lower.

        Returns:
        the current value as a long. Or 0 if there is no value
        Throws:
        java.lang.ArithmeticException - if the value in this field will not fit into a long
      • setValue

        public void setValue​(long newValue)
        Convenience method for setting the value to a long value;
        Parameters:
        newValue - the new value for the field.
      • setValue

        public void setValue​(int newValue)
        Convenience method for setting the value to an int value;
        Parameters:
        newValue - the new value for the field.
      • setValue

        public void setValue​(java.math.BigInteger newValue)
        Sets the value of the field to the given value. A null value will clear the field.
        Parameters:
        newValue - the new value or null.
      • setShowNumberMode

        public void setShowNumberMode​(boolean show)
        Turns on or off the faded text that displays the field's radix mode (hex or decimal).
        Parameters:
        show - true to show the radix mode.
      • setHexMode

        public void setHexMode()
        Sets the radix mode to Hex.

        If the field is currently in decimal mode, the current text will be change from displaying the current value from decimal to hex.

      • setDecimalMode

        public void setDecimalMode()
        Sets the mode to Decimal.

        If the field is currently in hex mode, the current text will be change from displaying the current value from hex to decimal.

      • setAllowsHexPrefix

        public void setAllowsHexPrefix​(boolean allowsHexPrefix)
        Sets whether on not the field supports the 0x prefix.

        If 0x is supported, hex numbers will be displayed with the 0x prefix. Also, when typing, you must type 0x first to enter a hex number, otherwise it will only allow digits 0-9. If the 0x prefix option is turned off, then hex numbers are displayed without the 0x prefix and you can't change the decimal/hex mode by typing 0x. The field will either be in decimal or hex mode and the typed text will be interpreted appropriately for the mode.

        Parameters:
        allowsHexPrefix - true to use the 0x convention for hex.
      • getText

        public java.lang.String getText()
        Returns the current text displayed in the field.
        Returns:
        the current text displayed in the field.
      • isHexMode

        public boolean isHexMode()
        Returns true if in hex mode, false if in decimal mode.
        Returns:
        true if in hex mode, false if in decimal mode.
      • setAllowNegativeValues

        public void setAllowNegativeValues​(boolean b)
        Sets whether or not negative numbers are accepted.
        Parameters:
        b - if true, negative numbers are allowed.
      • getMaxValue

        public java.math.BigInteger getMaxValue()
        Returns the current maximum allowed value. Null indicates that there is no maximum value.
        Returns:
        the current maximum value allowed.
      • setMaxValue

        public void setMaxValue​(java.math.BigInteger maxValue)
        Sets the maximum allowed value. The maximum must be a positive number. Null indicates that there is no maximum value.
        Parameters:
        maxValue - the maximum value to allow.
      • getComponent

        public javax.swing.JComponent getComponent()
        Returns the JTextField component that this class manages.
        Returns:
        the JTextField component that this class manages.
      • addActionListener

        public void addActionListener​(java.awt.event.ActionListener listener)
        Adds an ActionListener to the TextField.
        Parameters:
        listener - the ActionListener to add.
      • removeActionListener

        public void removeActionListener​(java.awt.event.ActionListener listener)
        Removes an ActionListener from the TextField.
        Parameters:
        listener - the ActionListener to remove.
      • setEnabled

        public void setEnabled​(boolean enabled)
        Sets the enablement on the JTextField component;
        Parameters:
        enabled - true for enabled, false for disabled.
      • setEditable

        public void setEditable​(boolean editable)
        Sets the editable mode for the JTextField component
        Parameters:
        editable - boolean flag, if true component is editable
      • requestFocus

        public void requestFocus()
        Requests focus to the JTextField
      • selectAll

        public void selectAll()
        Selects the text in the JTextField