【问题标题】:JTree drop index between nodes节点之间的 JTree 删除索引
【发布时间】:2012-04-06 08:22:24
【问题描述】:

我已经在我的 JTree 中实现了拖放并且它可以工作,但我觉得这有点令人困惑。在图片中,我展示了item4item5 之间的放置位置如何根据节点之间的距离而变化。我猜这是因为item4item5 的行边界在它们之间的中间相遇,并且根据你在哪一边,你实际上是在不同的行中。

从用户的角度来看,我认为这并不自然。我会认为,如果我跌落到一个节点之上,那么跌落就会发生在它之上。如果我下降到节点下方,下降将发生在它下方。有没有办法配置这种行为?

编辑:添加代码以显示如何获取放置位置

    DropLocation dropLoc = support.getDropLocation();
    Point dropPoint = dropLoc.getDropPoint();
    tree.getTree().getPathForLocation(dropPoint.x, dropPoint.y);

注意support 是一个TransferSupport 对象

编辑 2:我似乎通过检查下降点是高于还是低于节点的中点来解决这个问题。然后我可以判断下降是高于还是低于哪个节点。

【问题讨论】:

  • 你能告诉我们选择放置节​​点的代码吗?
  • 如需尽快获得更好的帮助,请发帖SSCCE
  • 我试图避免发布整个拖放代码,因为它是相当多的摆动代码,阅读起来可能很乏味。

标签: java swing drag-and-drop jtree


【解决方案1】:

你已经整理出了答案,但是由于我已经整理了代码,认为仍然值得发布它。

        public void dragOver(DropTargetDragEvent dtde)
        {
            Point dropPoint = dtde.getLocation();
            TreePath path = tree.getPathForLocation(dropPoint.x, dropPoint.y);
            Rectangle pathBounds = tree.getPathBounds(path);
            Rectangle topHalf = new Rectangle(pathBounds.x, pathBounds.y, pathBounds.width, pathBounds.height / 2);
            if (topHalf.contains(dropPoint))
            {
                System.out.println("top half");
            }
            else
            {
                System.out.println("bottom half");
            }
         }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-12-17
    • 1970-01-01
    • 2011-08-03
    • 2011-12-18
    • 1970-01-01
    • 2015-09-12
    • 2016-03-17
    相关资源
    最近更新 更多