JTree with popup menu

Hi all,

   I need to display popup menu when i click the tree nodes.. popup menu should contain the options delete node and rename node options.. so to delete or rename i should have the tree node object..
Thanks and Regards,
      JAgan
bvjaganAsked:
Who is Participating?
 
ozymandiasConnect With a Mentor Commented:
I have added rename and delete functions to the MenuItems.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.tree.*;

public class JTreePopupMenu extends JFrame{

      Container contentPane;
      JTree charTree;
      DefaultTreeModel treeModel;
      JScrollPane scroller;
      JSplitPane jsp;
      JPanel jp;
      JPopupMenu jpm;

      public JTreePopupMenu(String args[]){
            super("JTreePopupMenu");
            this.setDefaultCloseOperation(EXIT_ON_CLOSE);
            contentPane = this.getContentPane();
            contentPane.setLayout(new BorderLayout());
            treeModel = populateTree("ABCDCDDDDCCCDEEEDDDEEBCD");
            charTree = new JTree(treeModel);
            scroller = new JScrollPane(charTree);
            jp = new JPanel();
            jsp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,true,scroller,jp);
            jsp.setResizeWeight(0.4);
            contentPane.add(jsp,BorderLayout.CENTER);
            charTree.addMouseListener(new PopupListener());
            jpm = new JPopupMenu();
            JMenuItem deleteItem = new JMenuItem("Delete");
            JMenuItem renameItem = new JMenuItem("Rename");
            deleteItem.addActionListener(new PopupItemListener());
            renameItem.addActionListener(new PopupItemListener());
            jpm.add(renameItem);
            jpm.add(deleteItem);
      }

      public static void main(String args[]){
            JTreePopupMenu jtpm = new JTreePopupMenu(args);
            jtpm.setSize(600,600);
            jtpm.setVisible(true);
      }

      private void showPMenu(int x, int y){
            jpm.show(this,x,y);
      }


      private DefaultTreeModel populateTree(String charString){

            DefaultTreeModel treeModel;
            DefaultMutableTreeNode rootNode;
            rootNode = new DefaultMutableTreeNode("Characters");
            rootNode.removeAllChildren();
            char[] charArray = charString.toCharArray();

            DefaultMutableTreeNode a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z;
            a = new DefaultMutableTreeNode();
            b = new DefaultMutableTreeNode();
            c = new DefaultMutableTreeNode();
            d = new DefaultMutableTreeNode();
            e = new DefaultMutableTreeNode();
            f = new DefaultMutableTreeNode();
            g = new DefaultMutableTreeNode();
            h = new DefaultMutableTreeNode();
            i = new DefaultMutableTreeNode();
            j = new DefaultMutableTreeNode();
            k = new DefaultMutableTreeNode();
            l = new DefaultMutableTreeNode();
            m = new DefaultMutableTreeNode();
            n = new DefaultMutableTreeNode();
            o = new DefaultMutableTreeNode();
            p = new DefaultMutableTreeNode();
            q = new DefaultMutableTreeNode();
            r = new DefaultMutableTreeNode();
            s = new DefaultMutableTreeNode();
            t = new DefaultMutableTreeNode();
            u = new DefaultMutableTreeNode();
            v = new DefaultMutableTreeNode();
            w = new DefaultMutableTreeNode();
            x = new DefaultMutableTreeNode();
            y = new DefaultMutableTreeNode();
            z = new DefaultMutableTreeNode();

            for (int idx = 0; idx < charArray.length;idx++){
                  switch (Character.getNumericValue(charArray[idx])){
                        case 10:
                              rootNode.add(a = new DefaultMutableTreeNode("A"));
                              break;
                        case 11:
                              a.add(b = new DefaultMutableTreeNode("B"));
                              break;
                        case 12:
                              b.add(c = new DefaultMutableTreeNode("C"));
                              break;
                        case 13:
                              c.add(d = new DefaultMutableTreeNode("D"));
                              break;
                        case 14:
                              d.add(e = new DefaultMutableTreeNode("E"));
                              break;
                        case 15:
                              e.add(f = new DefaultMutableTreeNode("F"));
                              break;
                        case 16:
                              f.add(g = new DefaultMutableTreeNode("G"));
                              break;
                        case 17:
                              g.add(h = new DefaultMutableTreeNode("H"));
                              break;
                        case 18:
                              h.add(i = new DefaultMutableTreeNode("I"));
                              break;
                        case 19:
                              i.add(j = new DefaultMutableTreeNode("J"));
                              break;
                        case 20:
                              j.add(k = new DefaultMutableTreeNode("K"));
                              break;
                        case 21:
                              k.add(l = new DefaultMutableTreeNode("L"));
                              break;
                        case 22:
                              l.add(m = new DefaultMutableTreeNode("M"));
                              break;
                        case 23:
                              m.add(n = new DefaultMutableTreeNode("N"));
                              break;
                        case 24:
                              n.add(o = new DefaultMutableTreeNode("O"));
                              break;
                        case 25:
                              o.add(p = new DefaultMutableTreeNode("P"));
                              break;
                        case 26:
                              p.add(q = new DefaultMutableTreeNode("Q"));
                              break;
                        case 27:
                              q.add(r = new DefaultMutableTreeNode("R"));
                              break;
                        case 28:
                              r.add(s = new DefaultMutableTreeNode("S"));
                              break;
                        case 29:
                              s.add(t = new DefaultMutableTreeNode("T"));
                              break;
                        case 30:
                              t.add(u = new DefaultMutableTreeNode("U"));
                              break;
                        case 31:
                              u.add(v = new DefaultMutableTreeNode("V"));
                              break;
                        case 32:
                              v.add(w = new DefaultMutableTreeNode("W"));
                              break;
                        case 33:
                              w.add(x = new DefaultMutableTreeNode("X"));
                              break;
                        case 34:
                              x.add(y = new DefaultMutableTreeNode("Y"));
                              break;
                        case 35:
                              y.add(z = new DefaultMutableTreeNode("Z"));
                              break;
                  }
            }
            treeModel = new DefaultTreeModel(rootNode);
            return treeModel;
      }

      class PopupListener extends MouseAdapter{
            public void mouseClicked(MouseEvent me){
                  if (me.getModifiers() == me.BUTTON3_MASK){
                        JTree jt = (JTree)me.getSource();
                        TreePath tp = jt.getPathForLocation(me.getX(),me.getY());
                        if (tp != null){
                              jt.setSelectionPath(tp);
                              jpm.show(jt,me.getX(),me.getY());
                        }
                  }
            }
      }

      class PopupItemListener implements ActionListener{
            public void actionPerformed(ActionEvent ae){
                  if (((JMenuItem)ae.getSource()).getText() == "Delete"){
                        int response = JOptionPane.showConfirmDialog(charTree,"Are you sure you want to delete this node ?", "Confirm Delete",JOptionPane.YES_NO_CANCEL_OPTION);
                        if (response == JOptionPane.YES_OPTION){
                              MutableTreeNode tn = (MutableTreeNode)charTree.getSelectionPath().getLastPathComponent();
                              ((DefaultTreeModel)charTree.getModel()).removeNodeFromParent(tn);
                        }
                        return;
                  }
                  if (((JMenuItem)ae.getSource()).getText() == "Rename"){
                        String response = JOptionPane.showInputDialog(charTree, "Enter new node name :","Rename Node", JOptionPane.PLAIN_MESSAGE);
                        if (response != null && response.length() != 0){
                              MutableTreeNode tn = (MutableTreeNode)charTree.getSelectionPath().getLastPathComponent();
                              tn.setUserObject(response);
                              ((DefaultTreeModel)charTree.getModel()).reload(tn);
                        }
                        return;
                  }

            }
      }
}
0
 
ozymandiasCommented:
Sorry, this is a bit messy because I recycled some code from another question I was working on.

If you ignore the rather convoluted JTree and TreeModel set up and just look at the MouseListeners and Popup stuff you will see how to do it.


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.tree.*;

public class JTreePopupMenu extends JFrame{

      Container contentPane;
      JTree charTree;
      DefaultTreeModel treeModel;
      JScrollPane scroller;
      JSplitPane jsp;
      JPanel jp;
      JPopupMenu jpm;

      public JTreePopupMenu(String args[]){
            super("JTreePopupMenu");
            this.setDefaultCloseOperation(EXIT_ON_CLOSE);
            contentPane = this.getContentPane();
            contentPane.setLayout(new BorderLayout());
            treeModel = populateTree("ABCDCDDDDCCCDEEEDDDEEBCD");
            charTree = new JTree(treeModel);
            scroller = new JScrollPane(charTree);
            jp = new JPanel();
            jsp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,true,scroller,jp);
            jsp.setResizeWeight(0.4);
            contentPane.add(jsp,BorderLayout.CENTER);
            charTree.addMouseListener(new PopupListener());
            jpm = new JPopupMenu();
            JMenuItem deleteItem = new JMenuItem("Delete");
            JMenuItem renameItem = new JMenuItem("Rename");
            deleteItem.addActionListener(new PopupItemListener());
            renameItem.addActionListener(new PopupItemListener());
            jpm.add(renameItem);
            jpm.add(deleteItem);
      }

      public static void main(String args[]){
            JTreePopupMenu jtpm = new JTreePopupMenu(args);
            jtpm.setSize(600,600);
            jtpm.setVisible(true);
      }

      private void showPMenu(int x, int y){
            jpm.show(this,x,y);
      }


      private DefaultTreeModel populateTree(String charString){

            DefaultTreeModel treeModel;
            DefaultMutableTreeNode rootNode;
            rootNode = new DefaultMutableTreeNode("Characters");
            rootNode.removeAllChildren();
            char[] charArray = charString.toCharArray();

            DefaultMutableTreeNode a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z;
            a = new DefaultMutableTreeNode();
            b = new DefaultMutableTreeNode();
            c = new DefaultMutableTreeNode();
            d = new DefaultMutableTreeNode();
            e = new DefaultMutableTreeNode();
            f = new DefaultMutableTreeNode();
            g = new DefaultMutableTreeNode();
            h = new DefaultMutableTreeNode();
            i = new DefaultMutableTreeNode();
            j = new DefaultMutableTreeNode();
            k = new DefaultMutableTreeNode();
            l = new DefaultMutableTreeNode();
            m = new DefaultMutableTreeNode();
            n = new DefaultMutableTreeNode();
            o = new DefaultMutableTreeNode();
            p = new DefaultMutableTreeNode();
            q = new DefaultMutableTreeNode();
            r = new DefaultMutableTreeNode();
            s = new DefaultMutableTreeNode();
            t = new DefaultMutableTreeNode();
            u = new DefaultMutableTreeNode();
            v = new DefaultMutableTreeNode();
            w = new DefaultMutableTreeNode();
            x = new DefaultMutableTreeNode();
            y = new DefaultMutableTreeNode();
            z = new DefaultMutableTreeNode();

            for (int idx = 0; idx < charArray.length;idx++){
                  switch (Character.getNumericValue(charArray[idx])){
                        case 10:
                              rootNode.add(a = new DefaultMutableTreeNode("A"));
                              break;
                        case 11:
                              a.add(b = new DefaultMutableTreeNode("B"));
                              break;
                        case 12:
                              b.add(c = new DefaultMutableTreeNode("C"));
                              break;
                        case 13:
                              c.add(d = new DefaultMutableTreeNode("D"));
                              break;
                        case 14:
                              d.add(e = new DefaultMutableTreeNode("E"));
                              break;
                        case 15:
                              e.add(f = new DefaultMutableTreeNode("F"));
                              break;
                        case 16:
                              f.add(g = new DefaultMutableTreeNode("G"));
                              break;
                        case 17:
                              g.add(h = new DefaultMutableTreeNode("H"));
                              break;
                        case 18:
                              h.add(i = new DefaultMutableTreeNode("I"));
                              break;
                        case 19:
                              i.add(j = new DefaultMutableTreeNode("J"));
                              break;
                        case 20:
                              j.add(k = new DefaultMutableTreeNode("K"));
                              break;
                        case 21:
                              k.add(l = new DefaultMutableTreeNode("L"));
                              break;
                        case 22:
                              l.add(m = new DefaultMutableTreeNode("M"));
                              break;
                        case 23:
                              m.add(n = new DefaultMutableTreeNode("N"));
                              break;
                        case 24:
                              n.add(o = new DefaultMutableTreeNode("O"));
                              break;
                        case 25:
                              o.add(p = new DefaultMutableTreeNode("P"));
                              break;
                        case 26:
                              p.add(q = new DefaultMutableTreeNode("Q"));
                              break;
                        case 27:
                              q.add(r = new DefaultMutableTreeNode("R"));
                              break;
                        case 28:
                              r.add(s = new DefaultMutableTreeNode("S"));
                              break;
                        case 29:
                              s.add(t = new DefaultMutableTreeNode("T"));
                              break;
                        case 30:
                              t.add(u = new DefaultMutableTreeNode("U"));
                              break;
                        case 31:
                              u.add(v = new DefaultMutableTreeNode("V"));
                              break;
                        case 32:
                              v.add(w = new DefaultMutableTreeNode("W"));
                              break;
                        case 33:
                              w.add(x = new DefaultMutableTreeNode("X"));
                              break;
                        case 34:
                              x.add(y = new DefaultMutableTreeNode("Y"));
                              break;
                        case 35:
                              y.add(z = new DefaultMutableTreeNode("Z"));
                              break;
                  }
            }
            treeModel = new DefaultTreeModel(rootNode);
            return treeModel;
      }

      class PopupListener extends MouseAdapter{
            public void mouseClicked(MouseEvent me){
                  if (me.getModifiers() == me.BUTTON3_MASK){
                        JTree jt = (JTree)me.getSource();
                        TreePath tp = jt.getPathForLocation(me.getX(),me.getY());
                        if (tp != null){
                              jt.setSelectionPath(tp);
                              jpm.show(jt,me.getX(),me.getY());
                        }
                  }
            }
      }

      class PopupItemListener implements ActionListener{
            public void actionPerformed(ActionEvent ae){
                  JOptionPane.showMessageDialog(null,"You selected " + ((JMenuItem)ae.getSource()).getText());
            }
      }
}
0
 
bvjaganAuthor Commented:
Hi ozymandias,

   Thanks for your reply. here i am able to rename the node with setUserObject method..but i am not able to delete the node.. so for deletion what i have to do.. can u pls suggest me...

Thanks and Regards,
     Jagan
0
 
bvjaganAuthor Commented:
Hi ozymandias,

   Thank u very much for your help..
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.