Skip to main content

Overview

JNumberInput is a numeric input component with integrated increment (+) and decrement (−) buttons. It extends HBox and provides number validation, min/max constraints, step control, and integer-only mode.

Constructor

JNumberInput()
constructor
Creates a new JNumberInput with default value of 0.
JNumberInput numberInput = new JNumberInput();

Methods

Value Management

getValue()
double
Gets the current numeric value.
double currentValue = numberInput.getValue();
setValue(double value)
JNumberInput
Sets the numeric value. The value is automatically clamped between min and max.
numberInput.setValue(42);

Constraints

setMin(double min)
JNumberInput
Sets the minimum allowed value. If the current value is less than min, it will be set to min.
numberInput.setMin(0); // No negative values
setMax(double max)
JNumberInput
Sets the maximum allowed value. If the current value exceeds max, it will be set to max.
numberInput.setMax(100); // Cap at 100
setStep(double step)
JNumberInput
Sets the increment/decrement step size.
numberInput.setStep(0.5); // Increment by 0.5

Formatting

setIntegerOnly(boolean integerOnly)
JNumberInput
Sets whether to display and accept only integer values.
numberInput.setIntegerOnly(false); // Allow decimals

Event Handling

setOnValueChanged(Consumer<Double> callback)
JNumberInput
Sets a callback to be invoked whenever the value changes (via buttons or manual input).
numberInput.setOnValueChanged(value -> {
    System.out.println("Value changed to: " + value);
});

Additional Configuration

setPlaceholder(String placeholder)
JNumberInput
Sets the placeholder text for the input field.
numberInput.setPlaceholder("Enter quantity");
setNumberDisabled(boolean disabled)
JNumberInput
Enables or disables the entire input component.
numberInput.setNumberDisabled(true);
getTextField()
TextField
Gets the internal TextField for advanced customization.
TextField field = numberInput.getTextField();
field.setPrefWidth(150);

Style Classes

j-number-input
CSS class
Applied to the root HBox container
j-number-btn
CSS class
Applied to both increment and decrement buttons
j-number-btn-left
CSS class
Applied to the decrement button (−)
j-number-btn-right
CSS class
Applied to the increment button (+)
j-number-field
CSS class
Applied to the internal TextField

Input Validation

The component automatically validates input:
  • Only numeric characters, decimal points, and minus signs are allowed
  • Non-numeric characters are automatically filtered out
  • On focus lost or Enter press, the value is clamped to min/max range
  • Invalid input reverts to the last valid value

Usage Examples

Basic Quantity Input

import com.jjarroyo.components.JNumberInput;

public class QuantityExample {
    public JNumberInput createQuantityInput() {
        JNumberInput quantityInput = new JNumberInput();
        quantityInput.setValue(1)
                     .setMin(1)
                     .setMax(100)
                     .setStep(1)
                     .setPlaceholder("Qty")
                     .setOnValueChanged(value -> {
                         updateTotal((int) value.doubleValue());
                     });
        
        return quantityInput;
    }
    
    private void updateTotal(int quantity) {
        System.out.println("Quantity: " + quantity);
    }
}

Price Input with Decimals

import com.jjarroyo.components.JNumberInput;

public class PriceExample {
    public JNumberInput createPriceInput() {
        JNumberInput priceInput = new JNumberInput();
        priceInput.setValue(0.00)
                  .setMin(0)
                  .setMax(9999.99)
                  .setStep(0.01)
                  .setIntegerOnly(false)
                  .setPlaceholder("0.00")
                  .setOnValueChanged(price -> {
                      System.out.printf("Price: $%.2f%n", price);
                  });
        
        return priceInput;
    }
}

Age Input

import com.jjarroyo.components.JNumberInput;
import javafx.scene.layout.HBox;
import javafx.scene.control.Label;

public class AgeExample {
    public HBox createAgeInput() {
        HBox container = new HBox(10);
        
        Label label = new Label("Age:");
        
        JNumberInput ageInput = new JNumberInput();
        ageInput.setValue(18)
                .setMin(0)
                .setMax(120)
                .setStep(1)
                .setOnValueChanged(age -> {
                    validateAge(age.intValue());
                });
        
        container.getChildren().addAll(label, ageInput);
        return container;
    }
    
    private void validateAge(int age) {
        if (age < 18) {
            System.out.println("User is a minor");
        } else {
            System.out.println("User is an adult");
        }
    }
}

Temperature Input

import com.jjarroyo.components.JNumberInput;

public class TemperatureExample {
    public JNumberInput createTemperatureInput() {
        JNumberInput tempInput = new JNumberInput();
        tempInput.setValue(20)
                 .setMin(-50)
                 .setMax(50)
                 .setStep(0.5)
                 .setIntegerOnly(false)
                 .setPlaceholder("°C")
                 .setOnValueChanged(temp -> {
                     System.out.println("Temperature: " + temp + "°C");
                 });
        
        return tempInput;
    }
}

Disabled State

JNumberInput numberInput = new JNumberInput();
numberInput.setValue(50)
           .setMin(0)
           .setMax(100)
           .setNumberDisabled(true); // Disabled state

Behavior Details

Increment Button

  • Adds the step value to the current value
  • Respects the max constraint
  • Fires the onValueChanged callback if the value changes

Decrement Button

  • Subtracts the step value from the current value
  • Respects the min constraint
  • Fires the onValueChanged callback if the value changes

Manual Input

  • Users can type directly into the text field
  • Non-numeric characters are automatically filtered
  • Value is validated and clamped on Enter or focus loss
  • Invalid input reverts to the previous valid value

Value Clamping

All values are automatically clamped to the min/max range:
numberInput.setMin(0).setMax(10);
numberInput.setValue(15);  // Automatically becomes 10
numberInput.setValue(-5);  // Automatically becomes 0

See Also

Build docs developers (and LLMs) love