18 Replies Latest reply on Apr 25, 2007 2:16 PM by 187_2007

    Right-click on Tree item

      Is there any way to select a Tree node on a right click?
        • 1. Re: Right-click on Tree item
          ntsiii Level 3
          Right click, as you have noticed, brings up the FlashPlayer menu. You can add custom options to this menu, but you cannot, as far as i know, take over the right-click completely.

          • 2. Re: Right-click on Tree item
            LevV Level 1
            ntsiii, thanks for the reply. I got it figured out, can now add the custom options to the context menu and handle selection. Another question though: while hovering a tree item, the itemRollOver event is dispatched. Inside of this event listener, how would i get the reference to the hovered item? I can do that if I explicitely set the selected item on the Tree, but I'd rather not to do that. I guess i need something like "myTree.items[event.rowIndex]". Thanks in advance!

            // fires on itemRollOver event of the Tree
            private function onItemRollOver (event:ListEvent):void
            // myTree.selectedIndex = event.rowIndex; this works fine
            // event.currentTarget or event.target- reference to the Tree itself
            • 3. Re: Right-click on Tree item
              LevV Level 1
              In addition: tried myTree.getChildAt(event.rowIndex), getting a null reference exception.
              • 4. Re: Right-click on Tree item
                JabbyPandaUA Level 3
                You can use myTree.selectedItem

                Never use myTree.getChildAt to try to access the data displayed at tree rows, use myTree.dataProvider.getItemAt(index) instead

                Remember you want to access Tree's data, not underlying internal Sprites inside Tree structure.

                • 5. Re: Right-click on Tree item
                  LevV Level 1
                  JabbyPanda, thanks for the answer - that's what it was. I was probably trying it similar to myTree.selectedItem which gives me the entire the object that tree node is bound to.

                  Myabe you can give me a hand with another issue I'm having? How would you highligh (not select) a tree node on right click? The isHighlighted property is read only. Thanks againfor your response.
                  • 6. Re: Right-click on Tree item
                    JabbyPandaUA Level 3
                    Hi LevV,

                    First, I believe you were talking about public property "isItemHighlighted" , there is no "isHighlighted" property in Flex 2.0.1 SDK ;).

                    O K to resolve the task that you would like to perform ( on right click changing the background of the Tree row to another colour, not selected one) you will require an knowledge of how Tree Tree component is composed.

                    Study the code of mx.controls.listClasses.ListBase class.....

                    BTW, would you like to change the colour of the text or the colour of the background?
                    • 7. Re: Right-click on Tree item
                      LevV Level 1
                      Hi JabbyPanda,

                      Yes, that was the isItemHighlighted prop I was taking about :). Thanks, I'll look at the ListBase class code.

                      To answer your question - it's the background I want actualy not to change but to keep highlited when the right click is performed to pop upthe context menu (with Delete, Rename, New, etc. options). Highligh on the item goes away when you right click on a tree item.

                      Thansk again for your help!
                      • 8. Re: Right-click on Tree item
                        JabbyPandaUA Level 3
                        And one more question, as I understand you want to have an option in your context menu (which is actually open on the right button click) to perform a highlight of the Tree row.

                        Is this correct?
                        • 9. Re: Right-click on Tree item
                          JabbyPandaUA Level 3
                          See protected function mouseOverHandler(event:MouseEvent):void in ListBase class in a part:

                          Line 6833: var item:IListItemRenderer = mouseEventToItemRenderer(event);
                          Line 6849: if (getStyle("useRollOver") && (item.data != null))
                          if (allowDragSelection)
                          bSelectOnRelease = true;

                          var uid:String = itemToUID(item.data);
                          drawItem(visibleData[uid], isItemSelected(item.data), true, uid == caretUID);

                          You will have to subclass Tree in order to access methods "itemToUID", "mouseEventToItemRenderer"....
                          • 10. Re: Right-click on Tree item
                            LevV Level 1
                            HI JabbyPanda,

                            Thanks for the reference, I'll inherit from Tree and go from there.

                            To answer your question: I don't need an option in the context menu. I have a context menu in the tree with custom items in it that pops on right click. All works good, I can get a ferefence to the right clicked item and handle the selection...the only thing is that the higlight goes away from the node when right clicking on it and that's what I want to keep. Thanks again!
                            • 11. Re: Right-click on Tree item
                              JabbyPandaUA Level 3
                              Hi LevV,

                              You have to differentiate between two modes of Tree node item: SELECTED and HIGHLIGHTED.

                              <mx:Tree rollOverColor="#FF00000" selectionColor="#00FF00"/>

                              I believe you want to keep SELECTED colour highlight on the node.

                              That's a different story :)

                              BTW, does the colour of selection really goes away if you open context menu?

                              In my application it remains, see the the screenshot at link:
                              • 12. Re: Right-click on Tree item
                                LevV Level 1
                                Hi JabbyPanda,

                                You are right, I probably wasn't clear enough. In my case, I need the node to remain "highlited" (if this particular node is not already "selected") when I mouse over (the node highlights on mouse over) and then right-click on it. If another node is in the "selected" mode at the moment then it should remain selected. The highlight does go away on node right click.
                                • 13. Re: Right-click on Tree item
                                  LevV Level 1
                                  In addition to the previous message, following your suggestion: let's say the context menu has an option "open" and only when this option is selected then the node should switch its mode from higlighted to selected. Thanks!
                                  • 14. Re: Right-click on Tree item
                                    JabbyPandaUA Level 3
                                    OK, what about simplest possible solution that I can think of:

                                    override "mouseOutHandler" event handler with an empty function:

                                    //-------code starts---------------
                                    package com
                                    import flash.events.MouseEvent;
                                    import mx.controls.Tree;

                                    public class MyTree extends Tree
                                    public function MyTree()

                                    override protected function mouseOutHandler(event : MouseEvent):void
                                    //-------code ends---------------
                                    • 15. Re: Right-click on Tree item
                                      LevV Level 1
                                      Hi JabbyPanda,

                                      Thanks for the answer - this works like a charm. However, it looks like I'm going to need to select the node on right click...I created a subclass out of the Tree class to get access to the protected methods, but cannot find anything that fires on right click...mouseDown event won't fire when righ clicking. Any idea? Thanks!
                                      • 16. Re: Right-click on Tree item
                                        How did you select the treenode on right click?
                                        • 17. Re: Right-click on Tree item
                                          JabbyPandaUA Level 3
                                          It is not possible to select a treenode on right click, sorry.

                                          Flash does not have an mouse event for the right mouse click, right mouse click is reserved for showing context menu.

                                          But you can always select a treenode on left click :)
                                          • 18. Re: Right-click on Tree item
                                            187_2007 Level 1
                                            I know that this may be a little bt of a pain. But would you be willing to share your code that allowed you to add items to the Flash Menu? Or at least a link that will let us know how its done? I have seen this question a lot of times, but no real answer. People only say "I got it" and then you never hear from them again. :(