Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make JSpinner completely numeric

I've a Jspinner that can vary from minimum to maximum at steps of 0.1. This is working perfectly fine. Now, I set the editor of JSpinner as NumberEditor as the user can edit the textbox and I want only numeric values from this. This is also working, that is whatever the user may enter, the editor gives me only the numbers in the editor when I get the value using mySpinner.getValue().toString();. Now cones the problem. I want the textbox to accept only numeric values and .(decimal point), that is, if the user tries to enter anything apart from 0-9 and ., it should not echo it in the textbox.

JSpinner mySpinner = new JSpinner();
mySpinner.setModel(new SpinnerNumberModel(default,minimum,maximum,0.1));
mySpinner.setEditor(new JSpinner.NumberEditor(mySpinner,"##.#"));

Can someone help me with this. Thanks :)

like image 711
Balanivash Avatar asked Jun 23 '11 04:06

Balanivash


People also ask

How do I edit a number in a jspinner?

JSpinner.NumberEditor(JSpinner spinner, String decimalFormatPattern) Construct a JSpinner editor that supports displaying and editing the value of a SpinnerNumberModel with a JFormattedTextField. getFormat() Returns the java.text.DecimalFormat object the JFormattedTextField uses to parse and format numbers.

What is jspinner in Java Swing?

Java Swing | JSpinner. JSpinner is a part of javax.swing package . JSpinner contains a single line of input which might be a number or a object from a ordered sequence . The user can manually type in a legal data into the text field of the spinner . The spinner is sometimes preferred because they do not need a drop down list .

How to limit the values in a number jspinner component?

To limit the values in a number JSpinner component, use the SpinnerNumberModel that allows to set the min, max, step size and even the initial value − The following is an example to limit the values in a number JSpinner component −

What is the difference between jspinner and spinner?

The spinner is sometimes preferred because they do not need a drop down list. Spinners contain an upward and a downward arrow to show the previous and the next element when it is pressed. JSpinner (): Creates an empty spinner with an initial value set to zero and no constraints.


1 Answers

You could try setAllowsInvalid(false) as follows:

JFormattedTextField txt = ((JSpinner.NumberEditor) mySpinner.getEditor()).getTextField();
((NumberFormatter) txt.getFormatter()).setAllowsInvalid(false);

By setting this property, the Jspinner will show only valid values in the field and ignores all other values entered by the user. also the user will not be able to delete the whole value entered in the Jspinner i.e. if the user tries to select the entire value and delete it will not be allowed. User can edit the field only with a valid value other wise the it will seem like un-editable.

like image 167
Dilini Rajapaksha Avatar answered Sep 22 '22 14:09

Dilini Rajapaksha