jPopupMenu = new JPopupMenu();
jTree.add(jPopupMenu);
jTree.addMouseListener(new MouseAdapter()
{
public void mouseReleased(MouseEvent e)
{
try
{
//是否右键单击
if (e.getClickCount() == 1 && SwingUtilities.isRightMouseButton(e))
{
TreePath path = jTree.getPathForLocation(e.getX(), e.getY());
if (path == null)
return;
jTree.setSelectionPath(path);
jPopupMenu.show(jTree, e.getX(), e.getY());
}
else if (e.getClickCount() == 1)
{
TreePath path = jTree.getPathForLocation(e.getX(), e.getY());
if (path == null)
return;
if (jTree.isExpanded(path))
{
jTree.collapsePath(path);
}
else
{
jTree.expandPath(path);
}
return;
}
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
});
jTree.add(jPopupMenu);
jTree.addMouseListener(new MouseAdapter()
{
public void mouseReleased(MouseEvent e)
{
try
{
//是否右键单击
if (e.getClickCount() == 1 && SwingUtilities.isRightMouseButton(e))
{
TreePath path = jTree.getPathForLocation(e.getX(), e.getY());
if (path == null)
return;
jTree.setSelectionPath(path);
jPopupMenu.show(jTree, e.getX(), e.getY());
}
else if (e.getClickCount() == 1)
{
TreePath path = jTree.getPathForLocation(e.getX(), e.getY());
if (path == null)
return;
if (jTree.isExpanded(path))
{
jTree.collapsePath(path);
}
else
{
jTree.expandPath(path);
}
return;
}
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
});
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货