• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JSpinner and KeyListener: not working

 
Greenhorn
Posts: 9
Eclipse IDE Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've created a number-spinner class which should allow the user to press the + or - keys to increment or decrement the value. This is in addition to the normal up and down arrows on the control. (The idea is to have - another - keyboard control for changing the value.)

I've tried several variations, but can't seem to get the key handler to even be invoked. I've searched in this forum and elsewhere and have found a couple of similar descriptions, with responses like "oh yeah, it works now" (without much substance). Unfortunately, I still haven't reached that "oh yeah" part yet. Any ideas?

Thanks in advance...

I've extracted everything down to the smallest possible (and still broken) stand-alone example. Here's the code:

 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This makes use of the key bindings of the arrow keys and binds them to the numpad + and - keys. It's also necessary to consume the key typed events so that they aren't echoed in the spinner's editor.

I prefer to not extend Swing components (any class, really!) except where absolutely necessary, and favor a static utility method that returns a customized component.

edit: I'm not sure whether it's documented that listeners are invoked in the reverse of the order in which they are added, but AFAIK that's the behavior for all listeners added to Swing components, and this solution depends on it so as to consume the key typed events before they are processed by the UI-installed handler.
 
David A. Smith
Greenhorn
Posts: 9
Eclipse IDE Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Very interesting...! Thanks for your reply. I'm not familiar with the particular technique, so I'll have to play with it a bit.

Also, thanks for your comment on the order of listeners. I had wondered about that, but hadn't watched it that closely. I had another problem where I was trying to temporarily take listeners out of the loop so I could programatically set values without firing events to the listeners, but that's a different topic.

Dave.
 
Darryl Burke
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I'm not familiar with the particular technique, so I'll have to play with it a bit.


How to use Key Bindings
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic