Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

AbstractAction name with a associated KeyStroke  RSS feed

 
Juhan Voolaid
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have implemented a action class, that extends the AbstractAction and also I have associated a KeyStroke with that action.
So for example my OpenNewStuff action is triggered when Ctrl+O is pushed.

My problem is, that I want that the "Ctrl+O" binding information would be reflected in the Actions's name parameter.

Like for example in Firefox browser, when you push the "File" menu in the upper left corner, you see menu items labeled like so:
- New Window [space] Ctrl+N
- New Tab [space] Ctrl+T
- etc. ...

My OpenNewStuff action also is triggered by a menu item component and want the "Ctrl+O" to be added for the menu item's name.
How to do that correctly? Do I really have to set my actions name like that:
putValue(Action.NAME, "Open New Stuff" + " Ctrl+O"));

What about the space between? I think I cannot add "\t" character there.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What you are referring to, is called an accelerator key.
Check out Action#ACCELERATOR_KEY.
You will need to use it the same way using AbstractAction#putValue(Action.ACCELERATOR_KEY,Object keyValue)
More here on using accelerators.

Typically one uses the following
Action.NAME (just the display text like "Open")
Action.MNEMONIC_KEY (underlined 'O' in Open)
Action.ACCELERATOR_KEY (Ctrl+O)
 
Rob Spoor
Sheriff
Posts: 21095
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The value for MNEMONIC_KEY should be an Integer, e.g. Integer.valueOf('F').
The value for ACCELERATOR_KEY should be a KeyStroke.
And there are also SMALL_ICON and LARGE_ICON_KEY for the icon (both should take an Icon), SHORT_DESCRIPTION for the tooltip (should take a String), and ACTION_COMMAND_KEY for the action command (should take a String). You'll probably won't use the others. Check out the API for more information.
 
Juhan Voolaid
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Awesome! Thank you.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!