右键单击节点上的JTree和下拉选项

时间:2011-11-10 14:12:35

标签: swing popup jtree

我正在尝试使用JTree并为所有父节点和子节点实现不同的下拉列表。

这就是我所做的:

pmTree.addMouseListener(new java.awt.event.MouseAdapter() {
        @Override
        public void mouseClicked(java.awt.event.MouseEvent evt) {
            try {
            if(evt.getButton() == evt.BUTTON1) {
            }
            else if (evt.getButton() == evt.BUTTON3) {
                TreePopup(evt);
                //pmTree.updateUI();
            }
            }catch (Exception e) {}
        }
    });

和PopupCode:

public void TreePopup(java.awt.event.MouseEvent evt) {
    DefaultMutableTreeNode node = (DefaultMutableTreeNode)  pmTree.getLastSelectedPathComponent();
    popup = new JPopupMenu();
    popup.setInvoker(pmTree);
    PopupHandler handler = new PopupHandler(pmTree, popup);
    if(node.getLevel() == 1)
    {
        popup.add(getMenuItem("Parent Node", handler));
    }
    else if(node.getLevel() == 2)
    {
        popup.add(getMenuItem("Child", handler));
     }
     }

和PopUpHandler:

public class PopupHandler extends javax.swing.JFrame implements ActionListener {
JPopupMenu popup;
Point loc;

public PopupHandler(JTree tree, JPopupMenu popup) {
    //this.tree = NewJFrame.pmTree;
    this.popup = popup;
    tree.addMouseListener(ma);
}

以及

public void actionPerformed(java.awt.event.ActionEvent evt)  

单击Child或Parent节点。

但是,当我运行程序时,我得到了父节点的SAME右键单击弹出窗口。

对于大量的代码感到抱歉。我已经坚持了2天但却没有成功。 谢谢!

3 个答案:

答案 0 :(得分:3)

不要像MouseListener那样低级别,而是使用componentPopupMenu周围的api。这样做,一般方法是在getPopupLocation方法中动态配置componentPopup,这是一个简单的示例代码段:

    JPopupMenu popup = new JPopupMenu();
    final Action action = new AbstractAction("empty") {

        @Override
        public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub
        }
    };
    popup.add(action); 
    JTree tree = new JTree() {

        /** 
         * @inherited <p>
         */
        @Override
        public Point getPopupLocation(MouseEvent e) {
            if (e != null) {
               // here do your custom config, like f.i add/remove menu items based on context
               // this example simply changes the action name 
               TreePath path = getClosestPathForLocation(e.getX(), e.getY());
               action.putValue(Action.NAME, String.valueOf(path.getLastPathComponent()));
               return e.getPoint();
            }
            action.putValue(Action.NAME, "no mouse"); 
            return null;
        }

    };
    tree.setComponentPopupMenu(popup);

答案 1 :(得分:2)

您检查所选节点:

DefaultMutableTreeNode node = (DefaultMutableTreeNode)pmTree.getLastSelectedPathComponent();

查看您是否有“父”或“子”节点。您应该首先在鼠标位置选择节点,否则它将不是正确的节点。呼叫

TreePath path = pmTree.getPathForLocation(evt.getX(), evt.getY());
if (path != null) {
    pmTree.setSelectionPath(path);
} else {
    return;
}

treePopup的开头。 (Java中的方法应该以小写字母开头!)

答案 2 :(得分:1)

真棒。我成功地将setSelectionPath()调用置于getPopupLocaiton()的覆盖范围内。我一直试图在ActionListener的{​​{1}}内进行此操作,但无济于事。

JMenuItem