Meaningless Drivel is fun!*
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
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...
 
Consider Paul's rocket mass heater.
 
subject: How to prevent certain inputs in JSpinner?
 
Similar Threads
JSpinner question
JWhat
JSpinner DateModel : Set a date
JSpinner
how to Change JSpinner&JComboBox Arrows Color?