Flex Tip of the Day: accelerator decorations on menus
If your Flex application has a menu bar and there are control-key or command-key accelerators for items in the menu bar, you’d like to label your menu bar items with the accelerators. So, for example, if your File / Save command has a Control-S accelerator, you’d like the menu item to read “Save” with a “Ctrl-S” label to the right.
Trouble is, there’s no way to specify decorations in the framework menu classes. So here’s a quick cookbook on how to do it yourself. The basic approach is to subclass MenuItemRenderer and add an additional right-aligned UITextField to display your accelerator decoration.
1. In your menu XML, add an @acceleratorKey attribute to each menu item that has one. So, for example, the entry for Save might say <menuitem label=”Save” acceleratorKey=”s”/>.
2. Subclass MenuItemRenderer, and override createChildren(), measure(), updateDisplayList(), and the data setter function. In the createChildren() override, create a UITextField and add it as a child. In measure(), adjust measuredWidth by the textWidth of the UITextField. In updateDisplayList(), set your UITextField’s x position, and in the data setter, get the accelerator decoration text out of the value object and set it into your UITextField.
3. Subclass MenuBar, and override getMenuAt(). First chain up to the superclass, then set its menuItemRenderer to a ClassFactory that takes your MenuItemRenderer subclass as its parameter.
If you are supporting both Mac and Windows, it’s probably a good idea to write some code in your MenuBar subclass that figures out whether you’re running on Mac or Windows and updates the decoration prefix to “Ctrl-” or “Cmd-” accordingly.
Technorati Tags: Flex, Flex Tip of the Day



I'm working on Noteflight, an online music writing application that lets you create, view, print and hear music notation with professional quality, right in your web browser. Work on a score from any computer on the Internet, share it with other users, and embed it in your own pages. Noteflight is free for individual use. Email me at dcoletta at noteflight dot com.

Can you please share the source code with us?
Good suggestion. I’ll follow up with source in a later post.
It would be cool if you could show us how to add a style to a menuitem at the same time… right now, only a background color can be specified. All other skins/styles are ignored. This makes the menubar very skinnable, but its items not.
I did something like this a while ago except I handeled it a bit diferently using the icon function to dynamically render an icon
source included at:
http://blog.xsive.co.nz/archives/152
Styling is hard coded as this was a quick example but easy to implement with meta tags
Hi david,
Do you still plan on sharing the source code? I’d greatly appreciate it!
Sorry, my bad. I should have posted it when we were a standalone company. Now that we’re Adobe I have to go find out how to get permission…
Even if unable to post source, it’d be super helpful if you could expand or metacode this passage:
2. Subclass MenuItemRenderer, and override createChildren(), measure(), updateDisplayList(), and the data setter function. In the createChildren() override, create a UITextField and add it as a child. In measure(), adjust measuredWidth by the textWidth of the UITextField. In updateDisplayList(), set your UITextField’s x position, and in the data setter, get the accelerator decoration text out of the value object and set it into your UITextField.
very much appreciated
Great suggestions… I put together some sample code that accomplishes this.
http://www.rphelan.com/archives/7
No support for mac yet (even though I develop on a mac), but that’s a great idea.
Sorry, link changed: http://www.rphelan.com/2008/03/17/flex-menu-accelerators/.
As per steve, I seems a good way out. Has some one implemented his way ?