« Extending TreeItemRenderer in Flex 2 Beta 2 | Main | Quick Fix support and error highlighting plugin for Flex Builder 2 Beta 3 »

Custom context menus on Lists: Flex 2 Beta 2

Here's a simple example of how to add custom context menu items to your application.

It's a little awkward on a List-based component to capture the list item where the context menu was brought up.

I've heard of people capturing and storing the rollOver events on the list, and you then know that the last item rolled over is the one where the context menu was brought up.

But, if your lists itemRenderer implements mx.controls.listClasses.IDropInListItemRenderer, you can get the selectedIndex from the listData without having to pay attention to rollOver events.

This example will trace the selected list items label and rowIndex, and the caption of the selected context menu item.

Because I was using an itemRenderer it got a little weird once I was trying to get the target list item. If you right-click on the actual label in the list, the events mouseTarget is UITextField, but if you right-click on the renderer itself then the events mouseTarget is the listRenderer that you want. So here's how I check for that...

/* The user may click right on an object inside the renderer
* ... in our case it would be the label. If so, the target is the UITextField
*/
if( ! (target is listRenderer) )
{
  selectedListRenderer	 	= target.parent.parent;
				
  // otherwise they are clicking right on the renderer itself...
  // ( outside of the label )
} else {
  selectedListRenderer	 	= listRenderer(event.mouseTarget);
}
I haven't tested this with any other objects in the itemRenderer. It sure ould be nice if this worked no matter what the contents of the itemRenderer are.

Click here to view the source

Save This Page    

Comments

hi, thanks for this example, but I can't see the source. Is this example builded with beta 2?

Sorry about that, I just fixed it.

Are you a member of the GT Crew on the midnight prowl? This is BBLDD.

What? Luke-Dawg? Is this all of a sudden becoming a meet old friends site? Dang. Let's take this off-line.

Are you able to add more than 2 custom items to the context menu? I'm get 2 max. Strange. Thanks.

-James

Hello, excellent site, very rich in content
and correctly carefully thought out,
personally I found here much interesting
and useful

Hello! Excellent site! Searched a long ago

Post a comment

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)