我有一个带有自定义模型和自定义渲染器的 JTree。它们似乎工作正常,除了当我展开具有多个子节点的节点时,它将所有子节点绘制到同一个矩形,因此只有最后一个节点可见。我可以遍历子节点并将它们打印到控制台,但 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 实现上进行一些修复。另外,我需要纠正自己:我没有自定义模型,而是自定义 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;
}
}
}