如何向 JTree 添加间隙

发布于 2024-10-20 03:00:24 字数 857 浏览 2 评论 0原文

我的问题是

我正在尝试创建一个并排显示两棵树的组件。这两棵树通常非常相似,但可能有一两个差异。如果存在差异,即一个分支中有一个分支,但另一个分支没有,我希望没有分支的树为它以及每个丢失的子节点显示一个空白空间。

因此,它可能看起来像

left tree        right tree
------------     -------------
+ Root           + Root
|                |
--Child A        --Child A
|                | 
--Child B        |
|                |
--Child C        --Child C

使用自定义渲染器删除那些应该是间隙的行的文本和图标相对直接。然而,这仍然留下了连接孩子和父母垂直线的水平线。这是我的问题。

left tree        right tree
------------     -------------
+ Root           + Root
|                |
--Child A        --Child A
|                | 
--Child B        --  <--I want to remove this
|                |
--Child C        --Child C

在这个简单的例子中,它可能是可以承受的,但是当缺失的分支也有子节点时,我最终会得到很多连接间隙的小线。

我认为一个潜在的替代方案是为每棵树创建一个单列 JTreeTable 并空白掉缺失分支的单元格。尽管这意味着垂直线的一部分也会丢失。

任何帮助、想法或评论都将非常感激。谢谢。

my problem is this

I'm trying create a component that displays two tree side by side. The two trees will generally be very similar but will probably have one or two differences. Where there is a difference, ie a branch in one but not in the other, I would like the tree without the branch to show an empty space for it, and for each of its missing children too.

So it might look something like

left tree        right tree
------------     -------------
+ Root           + Root
|                |
--Child A        --Child A
|                | 
--Child B        |
|                |
--Child C        --Child C

Its relatively straight forward to remove the text and icon for those rows that should be gaps using a custom renderer. However, this still leaves the horizontal line that connects a child to the parents vertical line. And this is my problem.

left tree        right tree
------------     -------------
+ Root           + Root
|                |
--Child A        --Child A
|                | 
--Child B        --  <--I want to remove this
|                |
--Child C        --Child C

It's maybe bareable in this simple example but when the missing branch has children too I end up with lots of small lines connecting gaps.

A potential alternative I think is to create a one column JTreeTable for each tree and blank out the cells for the missing branches. Although this will mean that a portion of the vertical line will also be lost.

Any help, ideas or comments will be very much appriciated. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

十雾 2024-10-27 03:00:24

请考虑以下内容:

class ReticentTreeUI extends BasicTreeUI {

    private Set<Integer> hiddenRows = new HashSet<Integer>();

    public void hideRow(int row) {
        hiddenRows.add(row);
    }

    @Override
    protected void paintHorizontalPartOfLeg(Graphics g,
        Rectangle clipBounds, Insets insets, Rectangle bounds,
        TreePath path, int row, boolean isExpanded,
        boolean hasBeenExpanded, boolean isLeaf) {
        if (!hiddenRows.contains(row)) {
            super.paintHorizontalPartOfLeg(g, clipBounds, insets, bounds,
                path, row, isExpanded, hasBeenExpanded, isLeaf);
        }
    }

    @Override
    protected void paintRow(Graphics g, Rectangle clipBounds,
        Insets insets, Rectangle bounds, TreePath path, int row,
        boolean isExpanded, boolean hasBeenExpanded, boolean isLeaf) {
        if (!hiddenRows.contains(row)) {
            super.paintRow(g, clipBounds, insets, bounds, path, row,
                isExpanded, hasBeenExpanded, isLeaf);
        }
    }

    @Override
    protected void paintExpandControl(Graphics g, Rectangle clipBounds,
        Insets insets, Rectangle bounds, TreePath path, int row,
        boolean isExpanded, boolean hasBeenExpanded, boolean isLeaf) {
        if (!hiddenRows.contains(row)) {
            super.paintExpandControl(g, clipBounds, insets, bounds,
                path, row, isExpanded, hasBeenExpanded, isLeaf);
        }
    }
}

使用示例:

    JTree tree = new JTree();
    ReticentTreeUI ui = new ReticentTreeUI();
    tree.setUI(ui);
    ui.hideRow(2);

Consider the following:

class ReticentTreeUI extends BasicTreeUI {

    private Set<Integer> hiddenRows = new HashSet<Integer>();

    public void hideRow(int row) {
        hiddenRows.add(row);
    }

    @Override
    protected void paintHorizontalPartOfLeg(Graphics g,
        Rectangle clipBounds, Insets insets, Rectangle bounds,
        TreePath path, int row, boolean isExpanded,
        boolean hasBeenExpanded, boolean isLeaf) {
        if (!hiddenRows.contains(row)) {
            super.paintHorizontalPartOfLeg(g, clipBounds, insets, bounds,
                path, row, isExpanded, hasBeenExpanded, isLeaf);
        }
    }

    @Override
    protected void paintRow(Graphics g, Rectangle clipBounds,
        Insets insets, Rectangle bounds, TreePath path, int row,
        boolean isExpanded, boolean hasBeenExpanded, boolean isLeaf) {
        if (!hiddenRows.contains(row)) {
            super.paintRow(g, clipBounds, insets, bounds, path, row,
                isExpanded, hasBeenExpanded, isLeaf);
        }
    }

    @Override
    protected void paintExpandControl(Graphics g, Rectangle clipBounds,
        Insets insets, Rectangle bounds, TreePath path, int row,
        boolean isExpanded, boolean hasBeenExpanded, boolean isLeaf) {
        if (!hiddenRows.contains(row)) {
            super.paintExpandControl(g, clipBounds, insets, bounds,
                path, row, isExpanded, hasBeenExpanded, isLeaf);
        }
    }
}

Usage example:

    JTree tree = new JTree();
    ReticentTreeUI ui = new ReticentTreeUI();
    tree.setUI(ui);
    ui.hideRow(2);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文