Reputation: 1
Is it possible to add nodes just before expanding a tree, no matter how I approach this it just doesn't work at all. I've used all of the DefaultTreeModel's functions and it just doesn't seem to work, am I missing something or do I need to find another way around this?
public class Test {
public static void createChildren(FileNode pNode, DefaultTreeModel pModel) {
createChildren(pNode, pModel, 0);
}
public static void createChildren(FileNode pNode, DefaultTreeModel pModel, int pLayer) {
// if node doesn't exist
if (pNode == null) {
return;
}
// Get the root
File root = pNode.getFile();
File[] inDirectory;
// if we aren't in a directory or file doesn't exist
if (!root.isDirectory() || !root.exists()) {
return;
}
// List files in directory
inDirectory = root.listFiles();
// If there are no files, do not bother
if (inDirectory == null || !(inDirectory.length > 0)) {
return;
}
// If the node has children replace
if (pNode.getChildCount() > 0) {
// Delete ALL Nodes
deleteChildren(pNode, pModel);
}
// Then we will create the children
int[] inds = new int[inDirectory.length];
// i scales for ALL FILES, Index scales for DIRECTORIES ONLY
int index = 0;
for (File f : inDirectory) {
// Create a new child
FileNode child = new FileNode(f);
// Add nodes if we are in the layer to do so
if (pLayer < 99) {
createChildren(child, pModel, pLayer + 1);
}
// Add the node
if (pModel == null) {
pNode.add(child);
} else {
pModel.insertNodeInto(child, pNode, pNode.getChildCount());//index);
pModel.reload(child);
}
index++;
}
}
}
I've tried creating new nodes before and while expanding the node but the JTree just doesn't seem to update at all. (There is a function outside of this which has JTree validate and repaint.)
Upvotes: 0
Views: 22