[英]How to disable expand sign in Swing JTree?
我在Swing工作,我想在某种类型的节点上禁用扩展(加[+])符号。
不知道怎么做,因为我的节点不是叶子,我也不能使用setShowsRootHandles
(仅用于root)。
我指的是JTree:假设我得到了这个结构:
根
- [+] node1
- [+] node2
当我加载这个结构时,我不想在node2上看到[+]符号(因为它是一个特殊的类型节点)。 但我也想通过使用特殊命令来扩展它。
我重写了isLeaf()(来自DefaultMutableTreeNode的方法),所以当我在特殊类型节点时它会设置为TRUE,但是当我试图扩展它时,它不会扩展,因为isLeaf()= = TRUE ......
希望这会使事情变得更加清晰。
虽然无法移除句柄,但可以限制节点的扩展。 要走的路是TreeWillExpandListener与自定义treeNode结合使用,该树状态具有限制扩展的状态:
示例代码:
// mixed tree of normal/restricted noded
DefaultMutableTreeNode root = new DefaultMutableTreeNode("root");
DefaultMutableTreeNode normalSubTree = new DefaultMutableTreeNode("normal");
normalSubTree.add(new DefaultMutableTreeNode("normalChild"));
MyNode restrictedSubTree = new MyNode("restrictedSubtree");
restrictedSubTree.add(new DefaultMutableTreeNode("restrictedChild"));
root.add(normalSubTree);
root.add(restrictedSubTree);
final JTree tree = new JTree(root);
// the listener which vetos expansion of MyNodes that are not expandable
TreeWillExpandListener l = new TreeWillExpandListener() {
@Override
public void treeWillExpand(TreeExpansionEvent event)
throws ExpandVetoException {
TreePath path = event.getPath();
if (path.getLastPathComponent() instanceof MyNode) {
if (!((MyNode) path.getLastPathComponent()).isExpandable()) {
throw new ExpandVetoException(event, "node not expandable");
}
}
}
@Override
public void treeWillCollapse(TreeExpansionEvent event)
throws ExpandVetoException {
}
};
tree.addTreeWillExpandListener(l);
Action expand = new AbstractAction("Expand") {
@Override
public void actionPerformed(ActionEvent e) {
TreePath selected = tree.getSelectionPath();
if (selected == null) return;
if (selected.getLastPathComponent() instanceof MyNode) {
MyNode last = (MyNode) selected.getLastPathComponent();
boolean old = last.isExpandable();
last.setExpandable(true);
tree.expandPath(selected);
last.setExpandable(old);
}
}
};
JXFrame frame = wrapWithScrollingInFrame(tree, "veto expand");
addAction(frame, expand);
show(frame);
}
// custom node which has an expandable property
public static class MyNode extends DefaultMutableTreeNode {
private boolean expandable;
public MyNode() {
this(null);
}
public MyNode(Object userObject) {
super(userObject);
}
public void setExpandable(boolean expandable) {
this.expandable = expandable;
}
public boolean isExpandable() {
return expandable;
}
}
除非其他人提到过,否则可以移除手柄。 我已经附上了一个如何在下面执行此操作的片段。 关键是在BasicTreeUI
覆盖shouldPaintExpandControl
。
jtree.setUI(new BasicTreeUI() {
@Override
protected boolean shouldPaintExpandControl(final TreePath path, final int row
, final boolean isExpanded, final boolean hasBeenExpanded, final boolean isLeaf)
{
boolean shouldDisplayExpandControl = false;
return shouldDisplayExpandControl;
}
这应该在JTree API中记录,但这是另一个问题。
另一种考虑方法:
如果调用DefaultTreeModel(TreeNode root, boolean asksAllowsChildren)
,模型将“询问”您插入的节点(如果允许它们生成子节点)。 如果他们不能,则不应显示展开图标。
请务必在您的类中覆盖javax.swing.tree.TreeNode.getAllowsChildren()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.