We help IT Professionals succeed at work.

JTree question

GoldenLeaf
GoldenLeaf asked
on
I am new to Java Swing, can some one tell me how to build a dynamic tree that when one node is right clicked, a pop menu will be shown to add or delete a node?
Comment
Watch Question

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(4,2);
            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 addItem = new JMenuItem("Add");
            JMenuItem deleteItem = new JMenuItem("Delete");
            JMenuItem renameItem = new JMenuItem("Rename");
            addItem.addActionListener(new PopupItemListener());
            deleteItem.addActionListener(new PopupItemListener());
            renameItem.addActionListener(new PopupItemListener());
            jpm.add(addItem);
            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 void addRandomChildren(DefaultMutableTreeNode node, int min){
            int minval;
            if (min > 9){
                  minval = 9;
            }else{
                  minval = min;
            }
            int numChildren = (int)(Math.floor(Math.random() * (9 - minval)) + minval);
            String s = (String)node.getUserObject();
            for (int i = 1; i <= numChildren; i++){
                  node.add(new DefaultMutableTreeNode(s + "." + i));
            }
      }

      private DefaultTreeModel populateTree(int top, int depth){

            DefaultTreeModel treeModel;
            DefaultMutableTreeNode rootNode;
            DefaultMutableTreeNode firstNode;
            rootNode = new DefaultMutableTreeNode("Root");
            rootNode.removeAllChildren();
            for (int i = 0; i < top; i++){
                  firstNode = new DefaultMutableTreeNode("Node " + i);
                  rootNode.add(firstNode);
                  addRandomChildren(firstNode,3);
            }
            for (int j = 0; j < depth; j++){
                  DefaultMutableTreeNode leaf = new DefaultMutableTreeNode();
                  java.util.Enumeration e = rootNode.depthFirstEnumeration();
                  while (e.hasMoreElements()){
                        leaf = (DefaultMutableTreeNode)e.nextElement();
                        if (leaf.isLeaf()){
                              addRandomChildren(leaf,3);
                        }
                  }
            }
            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;
                  }
                  if (((JMenuItem)ae.getSource()).getText() == "Add"){
                        String response = JOptionPane.showInputDialog(charTree, "Enter new node name :","Add Node", JOptionPane.PLAIN_MESSAGE);
                        if (response != null && response.length() != 0){
                              DefaultMutableTreeNode nn = new DefaultMutableTreeNode(response);
                              DefaultMutableTreeNode tn = (DefaultMutableTreeNode)charTree.getSelectionPath().getLastPathComponent();
                              tn.add(nn);
                              ((DefaultTreeModel)charTree.getModel()).reload(tn);
                        }
                        return;
                  }


            }
      }
}
Hi,

If possible please send a sample program using xml. requirements are the following

1) It should run in the browser
2) it should take an xml file as an input, using that tree has to be builded
3) It should return xml file / stream

pls help in this regard

From

MRN Murthy
It is traditional to post new questions as new questions rather than tacking them on to the end of old ones.

If you have a requirement for code you will need to post your exact requirements as a new question in the Java topic area.

Bear in mind that what you are asking for is quite complex and, while they will be happy to help, few experts will simply write an applet of such complexity for you.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.