Solved

JTree with popup menu

Posted on 2001-06-29
4
733 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

744 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now