Results 1 to 2 of 2

Thread: JTree Popup

  1. #1
    jiexx is offline Member
    Join Date
    Mar 2009
    Posts
    4
    Rep Power
    0

    Default JTree Popup

    This triggers a popup when I click on a Jtree
    so when i select the Insert menuitem, i want to insert a new node at that position.. How can i access that node so i can create a child?

    Error:
    local variable node is accessed from within inner class; needs to be declared final

    Java Code:
    private void myPopupEvent(MouseEvent evt) {
                            int x = evt.getX();
                            int y = evt.getY();
                            JTree tree = (JTree)evt.getSource();
                            TreePath path = tree.getPathForLocation(x, y);
                            if (path == null)
                                    return; 
    
                            tree.setSelectionPath(path);
    
                            Object obj = path.getLastPathComponent();
                                                       
                            
                  DefaultMutableTreeNode node = DefaultMutableTreeNode)obj;
                             
                            JPopupMenu popup = new JPopupMenu();
                            JMenuItem m1 = new JMenuItem("Insert");
    
                            m1.addActionListener(new ActionListener(){
                            public void actionPerformed(ActionEvent e)
                            {
                                
         DefaultMutableTreeNode check = new DefaultMutableTreeNode("test");
    
                            node.add(check);            /////////// the problem is here.. how can i access the node ?
    
                            } });
                   }
    Thanks!

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    What happens if you do as the compiler suggests: make node final?

Similar Threads

  1. Move JTree item to another JTree.
    By Melki in forum AWT / Swing
    Replies: 8
    Last Post: 07-09-2009, 12:59 PM
  2. add PopUp menu by NetBeans
    By hungleon88 in forum AWT / Swing
    Replies: 1
    Last Post: 09-12-2008, 02:05 PM
  3. Replies: 7
    Last Post: 05-09-2008, 08:54 AM
  4. Communicating with JSP and popup
    By nilz in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 11-20-2007, 05:29 PM
  5. Popup in Java
    By fernando in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 07:55 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •