Class JIntegerField

  • All Implemented Interfaces:
    ImageObserver, MenuContainer, Serializable, Accessible, Scrollable, SwingConstants

    public class JIntegerField
    extends JValueField
    A text field control to work with integer values. It can be constrained to positive values if desired. It also provides an API for listening to value changes that is simpler than messing about with Swing KeyListener and DocumentListener.

    Example of use:

    
     int initialValue = ...
     boolean allowNegatives = false;
     JIntegerField control = new JIntegerField(initialValue, allowNegatives);
     control.addValueChangedListener( new ValueChangedListener() {
         public void onValueChanged( ValueChangedEvent ev ) {
             // System.out.println("The new value is " + ev.getValue());
         }
     });
     
    Since:
    2.6.1
    Author:
    Michael Bedward
    See Also:
    Serialized Form
    • Constructor Detail

      • JIntegerField

        public JIntegerField()
        Creates a new text field that allows negative values and has an initial value of 0.
      • JIntegerField

        public JIntegerField​(boolean allowsNegative)
        Creates a new text field with an initial value of 0.
        Parameters:
        allowsNegative - true if this field should allow negative values to be entered; false if only positive values are allowed
      • JIntegerField

        public JIntegerField​(int value)
        Creates a new text field that allows negative values and the given initial value.
        Parameters:
        value - the initial value to display
      • JIntegerField

        public JIntegerField​(int value,
                             boolean allowNegative)
        Creates a new text field with the given initial value.
        Parameters:
        value - the initial value to display
        allowNegative - true if this field should allow negative values to be entered; false if only positive values are allowed
    • Method Detail

      • getValue

        public int getValue()
        Get the current value of this control.
        Returns:
        the current value
      • setValue

        public void setValue​(int value)
        Set the integer value of this control. A ValueChangedEvent will be published to all ValueChangedListeners.
        Parameters:
        value - the value to set
        Throws:
        IllegalArgumentException - if value is negative but the field only allows positive values
      • setValue

        public void setValue​(int value,
                             boolean publishEvent)
        Set the integer value of this control, optionally skipping notification of the change to listeners.

        This version is useful when two or more controls are synchronized (ie. changes to the value of one control results in changes to the values of other controls). In such a setting, firing change events can result in an endless cycle or a mutex violation.

        Parameters:
        value - the value to set
        publishEvent - true to notify listeners of this change; false to skip notification
        Throws:
        IllegalArgumentException - if value is negative but the field only allows positive values