Class NumberTextField

All Implemented Interfaces:
FocusListener, ImageObserver, MenuContainer, Serializable, EventListener, Accessible, Scrollable, SwingConstants
Direct Known Subclasses:
PositiveNumberField

public class NumberTextField extends JFormattedTextField implements FocusListener

Provides a TextField component suitable for numbers. No error is produced when other characters are introduced, but just numbers are kept in the field after pressing enter or after focus is lost.

The component parses numbers according to the default Locale. For example when es_ES locale is active, a comma (",") is expected to separate the fractional part from the integer part, and when en_US is the default locale, then a dot (".") is expected to separate them.

The format of the accepted numbers can be modified by using the getFormat() method. For example, to get a TextField that accepts just integer numbes we would use:

    NumberTextField field = new NumberTextField();
    field.getFormat().setParseIntegerOnly(true);

In order to get a TextField that accepts double values with a minimum of two fractional digits and a maximum of five, we would use:

    NumberTextField field = new NumberTextField();
    field.getFormat().setMinimumFractionDigits(0);
    field.getFormat().setMaximumFractionDigits(5);
  

NumberTextField commits the value to the Field when the focus is lost, while standard JFormattedTextField just commits the value to the Field when the user presses ENTER.

See Also:
  • Constructor Details

    • NumberTextField

      public NumberTextField()
    • NumberTextField

      public NumberTextField(int columns)
    • NumberTextField

      public NumberTextField(double value, int columns)
    • NumberTextField

      public NumberTextField(int value, int columns)
  • Method Details

    • getFormat

      public NumberFormat getFormat()

      Gets the NumberFormat accepted by this text field. It can be used to change the format accepted by this text field.

      Examples:

      • To accept numbers with 0 to 5 fraction digits (as 2, 2.232, 6.23211):
           getFormat().setMinimumFractionDigits(0);
           getFormat().setMaximumFractionDigits(5);
      • To accept numbers with exactly two fraction digits (as 76.23, 12.00):
           getFormat().setMinimumFractionDigits(2);
           getFormat().setMaximumFractionDigits(2);
      • To accept just integer numbers (as 23, 5542, 0):
           getFormat().setParseIntegerOnly(true);
      • To enable digit grouping (enabled by default):
           getFormat().setGroupingUsed(true);

      Note: digit grouping enabled means to display 2534313.23 as 2,534,313.23

      Returns:
    • setValue

      public void setValue(double value)

      Sets the value of this TextField as a double primitive type.

      Parameters:
      value -
    • setValue

      public void setValue(int value)

      Sets the value of this TextField as an int primitive type.

    • setValue

      public void setValue(long value)

      Sets the value of this TextField as a long primitive type.

    • getDoubleValue

      public double getDoubleValue()

      Gets the value of this TextField as a double primitive type.

      Returns:
      The value as a double
    • getIntValue

      public int getIntValue()

      Gets the value of this TextField as an int primitive type.

      Returns:
      The value as an int
    • getLongValue

      public long getLongValue()

      Gets the value of this TextField as a long primitive type.

      Returns:
      The value as a long
    • focusGained

      public void focusGained(FocusEvent e)
      Specified by:
      focusGained in interface FocusListener
    • focusLost

      public void focusLost(FocusEvent e)
      Specified by:
      focusLost in interface FocusListener