aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes PopUp Menu for JTree Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "PopUp Menu for JTree" Watch "PopUp Menu for JTree" New topic
Author

PopUp Menu for JTree

sakthi anandan
Greenhorn

Joined: Feb 18, 2010
Posts: 3

I have a Jtree which on right click will show a pop up menu.

The problem is when i select any node in the tree with a left click and the right click anywhere in the frame that holds the JTree the pop up menu is Displayed.
I Want to display this pop up menu only when i right click on the selected Node in the JTree.

What am i to do to make this pop up work as intended ??


Thanks in Advance...
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
sakthi anandan wrote:
right click anywhere in the frame that holds the JTree the pop up menu is Displayed.


Anywhere in the frame? Or do you mean anywhere within the tree's scroll pane?


I Want to display this pop up menu only when i right click on the selected Node in the JTree.

What am i to do to make this pop up work as intended ??


What you do is examine the mouse event to get the x and y coordinates,
then use tree methods to make sure the click was actually over your node.

Consider:
  • MouseEvent.getPoint()
  • JTree.getClosestPathForLocation()
  • JTree.getPathBounds().contains() // but watch out for null
  • TreePath.getLastPathComponent()
  • etc.



  • bitguru blog
    sakthi anandan
    Greenhorn

    Joined: Feb 18, 2010
    Posts: 3
    The pop up appears when right clicked anywhere in the scroll pane containing the JTree.


    I am able to make the pop up appear only when right clicked on the node by using the mouseevent getx(), gety() and the components bound.
    But i want to know if there are any alternative way for doing the same.
    sakthi anandan
    Greenhorn

    Joined: Feb 18, 2010
    Posts: 3
    can somebody help me with this
     
    It is sorta covered in the JavaRanch Style Guide.
     
    subject: PopUp Menu for JTree