Right-click context menu for Java JTree?

后端 未结 6 1969
走了就别回头了
走了就别回头了 2020-12-24 12:07

I\'m trying to implement pop-up menus in Java JTree. I\'ve sub-classed DefaultTreeCellRenderer (to change node appearance) and DefaultTreeCellEditor (to create Components to

6条回答
  •  抹茶落季
    2020-12-24 13:01

    I think you're making things way harder than they need to be.
    JTree has several "add_foo_Listener" methods on it. Implement one of those (TreeSelectionListener looks about right), and then you've got the currently selected node.
    Implement a MouseListener so that you can detect the right-click event (and add it to the JTree, since JTree's a Component), and then you should have everything you need to post a context-sensitive menu.
    Check out this tutorial for more details.

提交回复
热议问题