Solved

JTree with popup menu

Posted on 2001-06-29
4
744 Views
Last Modified: 2011-10-03
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
0
Comment
Question by:bvjagan
  • 2
  • 2
4 Comments
 
LVL 15

Expert Comment

by:ozymandias
ID: 6241125
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
 

Author Comment

by:bvjagan
ID: 6244630
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
 
LVL 15

Accepted Solution

by:
ozymandias earned 100 total points
ID: 6244776
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
 

Author Comment

by:bvjagan
ID: 6251545
Hi ozymandias,

   Thank u very much for your help..
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Weekend adv creator 3 64
even odd program using while loop 3 72
hibernate jars 4 57
DTD and JAVA versions 1 28
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question