2
votes

I have a Flex application, running with Flash Player, not AIR, that contains a Tree that I would like to put a custom context menu on.

Tried just doing <mx:Tree ... contextMenu="{MyClassWithStatic.menu}">, but that didn't do anything.

Went searching, and found this quote from some Adobe docs somewhere

In Flex or Flash Builder, only top-level components in the application can have context menus. For example, if a DataGrid control is a child of a TabNavigator or VBox container, the DataGrid control cannot have its own context menu.

so went upwards, trying each parent element until I reached my <Application>-element, which is consistent with what they wrote.

Tried making a Flex component, based on Group (the default) which contained my tree, and the context menu on the top-level element there, hoping it would work, but to no avail.

Is there any other way to manage this that I haven't found yet?

The code I use to create the menu:

var menuItems:Array = [];
var rename:ContextMenuItem = new ContextMenuItem("Rename");
rename.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, renameSelectedHandler);  
menuItems.push(rename);  
menu.customItems = menuItems;
menu.hideBuiltInItems();
3
If what you basically want to do is have a context menu changed, only when you click on the tree, you should check for items under the mouse , on click, and if tree is one of them , should change the context menu.Neeraj
@Neeraj that is a solution I briefly considered. Feels a bit dirty and fragile, but unless I find anything better…PerfectlyNormal

3 Answers

2
votes

You're right, the contextmenu only works on top level components. It's a limitation of Flex which is annoying and shouldn't be there in the first place. There's not much you can do since there is no way to capture the event other than using some Javascript trickery, but even then, it doesn't tell you where you were clicking.

If I were you, I would just forget the concept and go away from using right click altogether if possible.

0
votes

I can't be sure, as all the code isn't' there. But you seem to have ignored your own research. Don't use your new component, or anything which "contains" your tree. Then just stick the Tree in your application.

Also I've a memory of TreeItemRenderer not being the same as in other UIcomponents. Maybe, test your "menu" code with a Datagrid first and make sure it works. Good luck

0
votes

I did not try it myself, but after reading the comments on http://michael.omnicypher.com/2007/02/flex-trees-with-context-menu_14.html it looks like you could add a context menu to the tree's item renderer.

The article and comments at http://blog.arc90.com/2008/04/21/adding-a-contextmenu-to-a-flex-tree/ are worth a look too.