Class PositiveNumberField

All Implemented Interfaces:
FocusListener, ImageObserver, MenuContainer, Serializable, EventListener, Accessible, Scrollable, SwingConstants

public class PositiveNumberField extends NumberTextField 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 NumberTextField.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

    • PositiveNumberField

      public PositiveNumberField()
    • PositiveNumberField

      public PositiveNumberField(int columns)
    • PositiveNumberField

      public PositiveNumberField(double value, int columns)
    • PositiveNumberField

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