具有相同边界的JTree子节点

时间:2012-09-07 14:30:08

标签: java swing jtree

我有一个带有自定义模型和自定义渲染器的JTree。它们似乎工作得很好,除了当我展开一个有多个子节点的节点时,它将所有子节点绘制到同一个矩形,因此只有最后一个节点是可见的。我可以遍历子节点,我可以将它们打印到控制台,但UI是不同的。

这是UI的草图:

// for 1 child node:
ROOT
-- <rendered node>

//for 2 child nodes:
ROOT
|
-- <rendered node - the data of the 2nd node>

//for 3 child nodes:
ROOT
|
|
-- <rendered node - the data of the 3rd node>

我发现BasicTreeUI使用getPathBounds()来确定某个节点的边界。我是这样检查的:

doStuffWithTree(JTree tree) {
   tree.expandNode(0);
   for (int i = 0; i < tree.getRowCount(); i++) {
       TreePath tp = tree.getPathForRow(i);
       tree.makeVisible(tp);
       System.out.println("Bounds of row " + i + ": " + tree.getPathBounds(tp));
   }
}

通过此检查,我得到所有节点 - 除了节点0 - 具有相同的边界。

我尝试了一些扩展树的技巧,例如从第一个开始扩展所有节点;然后从最后开始 - 它们都具有相同的效果。

最后,问题是:我可以修改哪个类来修复此行为?有没有已知的解决方法?

更新

显然,需要在TreeNode实现上修复某些内容。此外,我需要纠正自己:我没有自定义模型,但有一个cusomtom TreeNode。这是SSCCE:

package com.example.jtree;

//lots of obvious imports

public class JTreeBoundsExample extends JFrame {

public static void main(String... args) {
    JTreeBoundsExample m = new JTreeBoundsExample();
    m.setSize(275, 300);
    m.setDefaultCloseOperation(EXIT_ON_CLOSE);
    m.setVisible(true);
}

public JTreeBoundsExample() {
    ValueTreeNode root = new ValueTreeNode(null, "root");
    ValueTreeNode category = new ValueTreeNode(root, "category");
    category.put("k1", new ValueTreeNode(category, "v1"));
    category.put("k2", new ValueTreeNode(category, "v2"));
    category.put("k3", new ValueTreeNode(category, "v3"));
    root.put("category", category);

    JTree tree = new JTree(root);
    tree.setCellRenderer(new ValueCellRenderer());

    tree.setRootVisible(false);
    tree.expandRow(0);
    for (int i = 0; i < tree.getRowCount(); i++) {
        TreePath tp = tree.getPathForRow(i);
        tree.makeVisible(tp);
        System.out.println("Bounds of row " + i + ": " + tree.getPathBounds(tp));
    }

    Container content = getContentPane();
    content.add(new JScrollPane(tree), BorderLayout.CENTER);
}

private static class ValueTreeNode extends HashMap<String, ValueTreeNode> implements TreeNode {

    private List<String> keys = new ArrayList<String>();
    private TreeNode parent;
    private String value;

    public ValueTreeNode(TreeNode parent, String value) {
        this.parent = parent;
        this.value = value;
    }

    @Override
    public ValueTreeNode put(String key, ValueTreeNode val) {
        ValueTreeNode old = super.put(key, val);
        if (old == null) {
            sortKeys();
        }
        return old;
    }

    private void sortKeys() {
        keys.clear();
        keys.addAll(keySet());
        Collections.sort(keys);
    }

    @Override
    public Enumeration children() {
        final Iterator<String> it = keys.iterator();
        return new Enumeration() {
            @Override
            public boolean hasMoreElements() {
                return it.hasNext();
            }
            @Override
            public Object nextElement() {
                return it.next();
            }
        };
    }

    @Override
    public boolean getAllowsChildren() {
        return true;
    }

    @Override
    public TreeNode getChildAt(int pos) {
        return get(keys.get(pos));
    }

    @Override
    public int getChildCount() {
        return size();
    }

    @Override
    public int getIndex(TreeNode arg0) {
        return -1;
    }

    @Override
    public TreeNode getParent() {
        return parent;
    }

    @Override
    public boolean isLeaf() {
        return size() == 0;
    }
}

private static class ValueCellRenderer extends DefaultTreeCellRenderer {
    public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded,
            boolean leaf, int row, boolean hasFocus) {
        super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
        if (value instanceof ValueTreeNode) {
            ValueTreeNode vtn = (ValueTreeNode) value;
            setText(vtn.value);
        }
        return this;
    }
}   
}

0 个答案:

没有答案