java - Issue with JPopupMenu in a JTree -


I have this problem. In a custom JTree I used a JPP menu to display a different JNNEWITUM according to the selected node using a mouseover. Done The JPP menu is displayed when the mouse right button is clicked. The problem is that if I do not select an item from the popup menu, but instead I select the second node in the tree, either with the right or left button, this event is never caught by the tree MouseListener Can anyone point me in the right direction to solve it? If an example is available, then I would appreciate it.

I would like to select TreeSelectionListener to change the selected node as opposed to the MouseListener And repopulating JPopupMenu at that point, but this is your choice.

I'm trying to emulate my example, I was thinking, what methods did you override in your mouse listener? In this simple example, the listener seems to receive events whether or not the popup menu is visible

Edit -. See my comment below, but do not select a node right-click default behavior. This example chooses the nearest node where possible if right click was done.

  extended to public class sampling JFrame {Private JPopupMenu Menu = New JPopupMenu ("Popup"); Public Sampler () Headless Expansion {Super Throws ("Tree"); Final jetty tree = new jetty (); Tree.addMouseListener (New MouseAdapter) {Public Zero MouseReleased (if e.isPopupTrigger ()} {TreePath tp = tree.getClosestPathForLocation (e.getX (), e.getY ()); if (tp! = Null) {println (tp); tree.setSelectionPath (Tp);} menu.show (e.getComponent (), e.getX (), e.getY ());}}}); Letter letter = "abcdef"; (For the last four letters: letters.toCharArray ()) {Jemanuitam Item = New JMuitime (string.valof (letter)); Item.addActionListener (New ActionListener) {Public Zero ActionPerformed (ActionEvent E) {JOptionPane.showMessageDialog (SampleTree.this, "You have selected the letter:" + letter);}}); Menu.add (item); } Add (new JScrollPane (tree)); SetDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); } Public static void {String [] args) {SwingUtilities.invokeLater (New Runnable) {Public Zero Run} {SampleTree St = New SampleTree (); st.setSize (200, 200); st.setLocationRelativeTo (zero); St.setVisible (true);}}); }}  

Comments