Class JNumberInputField

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

public final class JNumberInputField extends JTextField implements ActionListener
The JNumberInputField class provides a input textfield for int and double datatypes.

Validation of the input is done via a ANumberFilter class and the setNumberFilter(ANumberFilter) method.

Any time focus is transferred, the field is validated; an action listener can be defined to catch these events.

Version:
04/12/2014
Author:
Sven Maerivoet
See Also:
  • Constructor Details

    • JNumberInputField

      public JNumberInputField(int integerValue, int fieldWidth, boolean autoCorrect, String errorMessage)
      Constructs a JNumberInputField for editing an int value.
      Parameters:
      integerValue - the initial int value in the input textfield
      fieldWidth - the field width
      autoCorrect - a boolean indicating whether or not the value should revert to the previous correct one in case of input error
      errorMessage - an optional error message that is displayed when an input error occurred
      See Also:
    • JNumberInputField

      public JNumberInputField(double doubleValue, int fieldWidth, boolean autoCorrect, String errorMessage)
      Constructs a JNumberInputField for editing a double value.
      Parameters:
      doubleValue - the initial double value in the input textfield
      fieldWidth - the field width
      autoCorrect - a boolean indicating whether or not the value should revert to the previous correct one in case of input error
      errorMessage - an optional error message that is displayed when an input error occurred
      See Also:
  • Method Details

    • setNumberFilter

      public void setNumberFilter(ANumberFilter numberFilter)
      Sets the filter to use for validation of the input.
      Parameters:
      numberFilter - a reference to the filter to use for validation of the input
    • getIntegerValue

      public int getIntegerValue()
      Retrieves the numerical int value of the textfield.

      Note that this method returns 0 when the JNumberInputField object operates on a double.

      Returns:
      the int value of the textfield
    • getDoubleValue

      public double getDoubleValue()
      Retrieves the numerical double value of the textfield.

      Note that this method returns 0.0 when the JNumberInputField object operates on a int.

      Returns:
      the double value of the textfield
    • actionPerformed

      public void actionPerformed(ActionEvent e)
      The JNumberInputField's action listener.

      This method always tries to transfer the focus to the next GUI component.

      Specified by:
      actionPerformed in interface ActionListener
      Parameters:
      e - the ActionEvent that is received.