Solved

Swing tree node context menu

Posted on 2010-11-08
6
1,021 Views
Last Modified: 2012-05-10
I have TreeNodes in a JTree.  I would like to add a popup context menu to each of the nodes.  This will popup when I right click on a node.  I do not intend to insert/delete nodes.  The context menu would have options such as Mark for Upload.  Can this context menu be added to a regular TreeNode or does it require DefaultMutableTreeNode?
0
Comment
Question by:mock5c
  • 3
  • 2
6 Comments
 
LVL 37

Expert Comment

by:zzynx
ID: 34092799
Remark that TreeNode is no class but an interface.

>> Can this context menu be added to a regular TreeNode
Yes.

>> or does it require DefaultMutableTreeNode?
No. But it can make your life easier to use DefaultMutableTreeNode and in that sense I would recommended it.

0
 

Author Comment

by:mock5c
ID: 34092924
In that case, is it straightforward to cast TreeNode to DefaultMutableTreeNode?  TreeNodes are used heavily in the code.
0
 
LVL 37

Accepted Solution

by:
zzynx earned 125 total points
ID: 34093044
>> In that case, is it straightforward to cast TreeNode to DefaultMutableTreeNode?
Don't understand.
Make sure you have DefaultMutableTreeNode's in your tree (by extending your MyTreeNode from DefaultMutableTreeNode) and you're done. No need to cast.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 1

Expert Comment

by:GenroseSusai
ID: 34093791
Tree with popup
___________________

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 WindowCloser());
    }
}

class WindowCloser extends WindowAdapter {
    public void windowClosing(WindowEvent e) {
        Window win = e.getWindow();
        win.setVisible(false);
        System.exit(0);
    }
}

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);
       
        addMouseListener(
                new MouseAdapter() {
            public void mouseReleased( MouseEvent e ) {
                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 structure.
            ((DefaultTreeModel )this.getModel()).nodeStructureChanged((TreeNode)dmtn);
        }
        if (ae.getActionCommand().equals("remove")) {
            node = (DefaultMutableTreeNode)dmtn.getParent();
            // Bug fix by essam
            int nodeIndex=node.getIndex(dmtn); // declare an integer to hold the selected nodes index
            dmtn.removeAllChildren();          // remove any children of selected node
            node.remove(nodeIndex);            // remove the selected node, retain its siblings
            ((DefaultTreeModel )this.getModel()).nodeStructureChanged((TreeNode)dmtn);       }
    }
}
0
 

Author Comment

by:mock5c
ID: 34097730
>> In that case, is it straightforward to cast TreeNode to DefaultMutableTreeNode?
>Don't understand.
>Make sure you have DefaultMutableTreeNode's in your tree (by extending your MyTreeNode from >DefaultMutableTreeNode) and you're done. No need to cast.

I have a class MyTreeNode implements TreeNode.  Should I just change that to MyTreeNode extends DefaultMutableTreeNode and remove the implementation of the TreeNode functions that I don't need/use?
0
 
LVL 37

Expert Comment

by:zzynx
ID: 34102937
>> I have a class MyTreeNode implements TreeNode.  
>> Should I just change that to MyTreeNode extends DefaultMutableTreeNode and remove the implementation of the TreeNode functions that I don't need/use?
Indeed.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

914 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

14 Experts available now in Live!

Get 1:1 Help Now