通过实现自定义CellRenderer,我有一个带有绿叶节点和红叶节点的jtree。
我这样做是为了扩展整个jtree:
expAll.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
for (int i = 0; i < jTree1.getRowCount(); i++) {
jTree1.expandRow(i);
}
}
});
但是如何创建一个动作来以编程方式将jtree扩展为仅具有Red单元的叶节点?
最佳答案
感谢这里的最终代码:
expFail.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//Exand the root node
jTree1.expandRow(0);
//Getting the jtree row from the root containing my red and green leaf node
DefaultMutableTreeNode dirNode = (DefaultMutableTreeNode) jTree1.getPathForRow(2).getLastPathComponent();
Enumeration enumDir = dirNode.preorderEnumeration();
while (enumDir.hasMoreElements()) {
//Walking through all nodes of dirNode
DefaultMutableTreeNode enumDirNode = (DefaultMutableTreeNode) enumDir.nextElement();
//If node is leaf and is red (returned by isFailed()) expand to the previous node of this leaf node
if (enumDirNode.isLeaf() && !((LeafNodeObject) enumDirNode.getUserObject()).isFailed()) {
jTree1.expandPath(new javax.swing.tree.TreePath(enumDirNode.getPreviousNode().getPath()));
}
}
}
});