Solved

JTree with popup menu

Posted on 2001-06-29
4
743 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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone 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
Maven Project: Hibernate Dependencies Conflict 10 38
difference of if loops 23 60
mysql jsp example issue 32 48
Eclipse for Java EE development 2 26
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

790 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