开发者

Create a numeric text box in java Swing with increment and decrement buttons

开发者 https://www.devze.com 2023-03-15 00:54 出处:网络
How can I create a numeric text box in java swing , which has two buttons (up and down) which increments and decrements the value in the text box respectively. Also this text box must be editable with

How can I create a numeric text box in java swing , which has two buttons (up and down) which increments and decrements the value in the text box respectively. Also this text box must be editable with ONLY NUMERIC VALUES. Something like this

Create a numeric text box in java Swing with increment and decrement buttons

I tried by placing two buttons near a text box and manually doing the operation on button cli开发者_StackOverflow社区ck.

Create a numeric text box in java Swing with increment and decrement buttons

Is there any other method in which I can do this in a better way and achieve a similar result as the first image.

Thanks :)


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);


JSpinner need for allows numeric input only, required some hack for that in its Model, but your 2nd. picture looks like as two JButtons (with JButton#setFocucPainted(false)), and one JFormattedTextField with number Format, 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)
0

精彩评论

暂无评论...
验证码 换一张
取 消