• 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

Swing Key bindings

 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Swing now allows for an abstract mapping between KeyStroke's and Action's by setting up the InputMap and ActionMap for the JComponent in question. One problem I faced recently goes something like this.
Suppose we mapped several KeyStrokes to the same ActionMap key like so...


...how does the Action Object associated with 'myActionKey' determine exactly which KeyStroke triggered it.
As obvious from the code, I was toying with a Calculator model. Initially I tried pulling it out of the ActionCommand of the ActionEvent, but that doesn't form a sufficiently unique pattern, for example the two mappings above generate an ActionCommand of "+", but I intended to create a generic Action to handle the contract for all functions in the calculator (=, -, *, /, memory, etc.) with actual functionality abstracted into implementations of a Function interface.
I managed a rather bulky workaround, but had to redesign significantly. My original design was to maintain a KeyStroke to Function factory map that could be used to manufacture Function instances corresponding to KeyStrokes generated.
So, getting back to the point, do any of u guys have any idea as to how we can derive the KeyStroke the triggered a particular Action?
 
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
saager mhatre
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, first of all, I said i wanted only one entry in the ActionMap. Something like...

But, that's OK since you used the same instace for all 4 mappings.

What I have ral reservations about is wiring into the KeyboardFocusManager's KeyEventDispatch'er just to capture the character typed. I could have done that by registering keystrokes using

I didn't want to use this factory method since I want to provide other Functions the scope to register KeyStrokes with modifiers.
Besides, my 'bulky' workaround turned out to be better than this and I also managed to find the more appropriate solution to the problem.
I'll tell you more about that in the next post. Gotta run now!
 
Can you shoot lasers out of your eyes? Don't look at this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic