Creating a text field in java Swing with increase and decrease buttons

How to create a numeric text field in java swing, which has two buttons (up and down), which increases and decreases the value in the text field, respectively. Also, this text box should be accessible using NUMERICAL VALUES ONLY . Something like that

Numeric text box

I tried to place two buttons next to the text box and manually perform the operation when the button is clicked.

My try text box

Is there any other method in which I can do it better and get a similar result with the first image.

Thanks:)

+7
source share
2 answers

Use JSpinner

How to use spinners in java


Based on your comment:

SpinnerModel model = new SpinnerNumberModel(9.9, 1, 15, 0.1); JSpinner spinner = new JSpinner(model); 
+14
source

JSpinner is required to enter only numerical input, it takes some hack for it in its model, but your second. the image looks like two JButtons (with JButton # setFocucPainted (false)) and one JFormattedTextField with the Format number, with min / maxDecimalPaces, with roundingMode

 myDoubleFormat.setMinimumFractionDigits(2); myDoubleForma.setMaximumFractionDigits(2); myDoubleForma.setRoundingMode(RoundingMode.HALF_UP); 

then Action from JButton will be

 myFtdTextField.setValue(((Number) myFtdTextField.getValue()).doubleValue() +/- 0.1) 
+2
source

All Articles