• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to prevent certain inputs in JSpinner?

 
Biliang Zhou
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
((DefaultFormatter)((JSpinner.DefaultEditor)spinner.getEditor()).getTextField().getFormatter()).setAllowsInvalid(false);
 
Biliang Zhou
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Man, I admire you...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic