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
Creates a new JNumberInput with default value of 0. JNumberInput numberInput = new JNumberInput ();
Methods
Value Management
Gets the current numeric value. The current numeric value
double currentValue = numberInput . getValue ();
Sets the numeric value. The value is automatically clamped between min and max. Returns this instance for method chaining
numberInput . setValue ( 42 );
Constraints
Sets the minimum allowed value. If the current value is less than min, it will be set to min. The minimum value (default: Double.NEGATIVE_INFINITY)
Returns this instance for method chaining
numberInput . setMin ( 0 ); // No negative values
Sets the maximum allowed value. If the current value exceeds max, it will be set to max. The maximum value (default: Double.POSITIVE_INFINITY)
Returns this instance for method chaining
numberInput . setMax ( 100 ); // Cap at 100
Sets the increment/decrement step size. The amount to add/subtract when using buttons (default: 1)
Returns this instance for method chaining
numberInput . setStep ( 0.5 ); // Increment by 0.5
Formatting
setIntegerOnly(boolean integerOnly)
Sets whether to display and accept only integer values. True for integer mode, false for decimal mode (default: true)
Returns this instance for method chaining
numberInput . setIntegerOnly ( false ); // Allow decimals
Event Handling
setOnValueChanged(Consumer<Double> callback)
Sets a callback to be invoked whenever the value changes (via buttons or manual input). The callback function that receives the new value
Returns this instance for method chaining
numberInput . setOnValueChanged (value -> {
System . out . println ( "Value changed to: " + value);
});
Additional Configuration
setPlaceholder(String placeholder)
Sets the placeholder text for the input field. The placeholder text to display when empty
Returns this instance for method chaining
numberInput . setPlaceholder ( "Enter quantity" );
setNumberDisabled(boolean disabled)
Enables or disables the entire input component. True to disable, false to enable
Returns this instance for method chaining
numberInput . setNumberDisabled ( true );
Gets the internal TextField for advanced customization. The internal TextField component
TextField field = numberInput . getTextField ();
field . setPrefWidth ( 150 );
Style Classes
Applied to the root HBox container
Applied to both increment and decrement buttons
Applied to the decrement button (−)
Applied to the increment button (+)
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