Have a popup attached to a JTreeTag(s): Swing


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JComponent;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;

public class MyJTree extends JTree implements ActionListener{
  JPopupMenu popup;
  JMenuItem mi;

  MyJTree (DefaultMutableTreeNode dmtn) {
   super(dmtn);
   // define the popup
   popup = new JPopupMenu();
   mi = new JMenuItem("Insert a children");
   mi.addActionListener(this);
   mi.setActionCommand("insert");
   popup.add(mi);
   mi = new JMenuItem("Remove this node");
   mi.addActionListener(this);
   mi.setActionCommand("remove");
   popup.add(mi);  
   popup.setOpaque(true);
   popup.setLightWeightPopupEnabled(true);

   final JTree thisTree = this;
   addMouseListener (
       new MouseAdapter () {
          public void mouseReleased( MouseEvent e ) {
            // thanks to urbanq for the bug fix!
            int row = thisTree.getRowForLocation(e.getX(), e.getY());
            if(row == -1)
              return;              
            thisTree.setSelectionRow(row);
             if ( e.isPopupTrigger()) {
                 popup.show( (JComponent)e.getSource(), 
                                e.getX(), e.getY() );
                 }
             } 
          }
       );
    
  }
 public void actionPerformed(ActionEvent ae) {
   DefaultMutableTreeNode dmtn, node;

   TreePath path = this.getSelectionPath();
   dmtn = (DefaultMutableTreeNode) path.getLastPathComponent();
   if (ae.getActionCommand().equals("insert")) {
     node = new DefaultMutableTreeNode("children");
     dmtn.add(node);
     // thanks to Yong Zhang for the tip for refreshing the tree struct
     ((DefaultTreeModel )this.getModel())
          .nodeStructureChanged((TreeNode)dmtn); 
     }
   if (ae.getActionCommand().equals("remove")) {
     node = (DefaultMutableTreeNode)dmtn.getParent();
     node.removeAllChildren();
     ((DefaultTreeModel )this.getModel())
          .nodeStructureChanged((TreeNode)dmtn); 
     }
   }
}

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.*;
import javax.swing.tree.*;

public class TreeWithPopup extends JPanel{ 
    DefaultMutableTreeNode root, node1, node2, node3;
    public TreeWithPopup() {
      MyJTree tree;
      root = new DefaultMutableTreeNode("root", true);
      node1 = new DefaultMutableTreeNode("node 1", true);
      node2 = new DefaultMutableTreeNode("node 2" , true);
      node3 = new DefaultMutableTreeNode("node 3", true);
      root.add(node1);
      node1.add(node2);
      root.add(node3);
      setLayout(new BorderLayout());
      tree = new MyJTree(root);
      add(new JScrollPane((JTree)tree),"Center");
    }

    public Dimension getPreferredSize(){
      return new Dimension(300, 300);
    }
     
    public static void main(String s[]){
      JFrame frame = new JFrame("Tree With Popup");
      TreeWithPopup panel = new TreeWithPopup();
      frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
      frame.setForeground(Color.black);
      frame.setBackground(Color.lightGray);
      frame.getContentPane().add(panel,"Center");
      frame.setSize(panel.getPreferredSize());
      frame.setVisible(true);
      frame.addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
          Window win = e.getWindow();
          win.setVisible(false);
          System.exit(0);
          }
      });
    }
 }


blog comments powered by Disqus