• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

PopUp Menu for JTree

 
sakthi anandan
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 886
1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.


  •  
    sakthi anandan
    Greenhorn
    Posts: 3
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 3
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    can somebody help me with this
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic