tree.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e){
if (e.getButton() == MouseEvent.BUTTON3) {
//此函数得到距鼠标点击最近处的节点行数
int row = tree.getClosestRowForLocation(e.getX(), e.getY());
//此函数得到距鼠标点击最近处的节点路径
TreePath treePath =
tree.getClosestPathForLocation(e.getX(), e.getY());
//判断鼠标点击处的坐标是否在所选节点的Rectangle之内
if (tree.getPathBounds(treePath).contains(e.getX(), e.getY())){
//设置所选节点为当前节点
tree.setSelectionRow(row);
DefaultMutableTreeNode node =
(DefaultMutableTreeNode)tree.getLastSelectedPathComponent();
if (node == null) return;
//判断该节点是否是叶子节点
if (node.isLeaf()){
//弹出对话框
popupMenu.show(e.getComponent(),e.getX(),e.getY());
}
}
}
}
});//that's ok!
public void mouseClicked(MouseEvent e){
if (e.getButton() == MouseEvent.BUTTON3) {
//此函数得到距鼠标点击最近处的节点行数
int row = tree.getClosestRowForLocation(e.getX(), e.getY());
//此函数得到距鼠标点击最近处的节点路径
TreePath treePath =
tree.getClosestPathForLocation(e.getX(), e.getY());
//判断鼠标点击处的坐标是否在所选节点的Rectangle之内
if (tree.getPathBounds(treePath).contains(e.getX(), e.getY())){
//设置所选节点为当前节点
tree.setSelectionRow(row);
DefaultMutableTreeNode node =
(DefaultMutableTreeNode)tree.getLastSelectedPathComponent();
if (node == null) return;
//判断该节点是否是叶子节点
if (node.isLeaf()){
//弹出对话框
popupMenu.show(e.getComponent(),e.getX(),e.getY());
}
}
}
}
});//that's ok!
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货