Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Swing tree node context menu

Posted on 2010-11-08
6
Medium Priority
?
1,041 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 500 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Suggested Courses

916 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