Solved

JTree with popup menu

Posted on 2001-06-29
4
754 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
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…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Suggested Courses

623 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