jQuery in Action, 2nd edition*
The moose likes Swing / AWT / SWT and the fly likes How to prevent certain inputs in JSpinner? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How to prevent certain inputs in JSpinner?" Watch "How to prevent certain inputs in JSpinner?" New topic
Author

How to prevent certain inputs in JSpinner?

Biliang Zhou
Ranch Hand

Joined: Jun 28, 2006
Posts: 43
JSpinner provides us with both a display field and a pair of up and down buttons. Both are kept in sync with the SpinnerModel and are able to change the value.

Now the problem is: if the JSpinner is intended to be used to handle nothing but integers, how to prevent user from inputting characters?

I tried to use spinner.getEditor(), and added KeyEventListener to the editor, but it didn't turn out to be successful.

Gurus, help me...
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
((DefaultFormatter)((JSpinner.DefaultEditor)spinner.getEditor()).getTextField().getFormatter()).setAllowsInvalid(false);
Biliang Zhou
Ranch Hand

Joined: Jun 28, 2006
Posts: 43
Man, I admire you...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to prevent certain inputs in JSpinner?
 
Similar Threads
JSpinner question
JWhat
JSpinner DateModel : Set a date
how to Change JSpinner&JComboBox Arrows Color?
JSpinner