问题
I'm using Wicket's Tree component in a web app. But empty folders are shown in a file-way. Just like this:

Bellow is where I use the DefaultTreeModel and Tree:
PDMinterface pdmI = new PDMinterface();
DefaultMutabletreeNode rootTreeNode = pdmI.getDocTree(); //文档树根结点,由PDM接口提供
DefaultTreeModel treeModel = new DefaultTreeModel(rootTreeNode);
treeModel.setAsksAllowsChildren(true);
and I'm sure that folder5 is set to allow children:
public DefaultMutableTreeNode getDocTree(){
DefaultMutableTreeNode root = new DefaultMutableTreeNode();
root.setAllowsChildren(true);
FolderNode rootFolder = new FolderNode(0, "root", "Jiajun", true);
root.setUserObject(rootFolder);
for(int i=0; i < 5; i++){
DefaultMutableTreeNode newnode = new DefaultMutableTreeNode();
newnode.setAllowsChildren(true);
FolderNode newFolder = new FolderNode(i+1, "Folder" + Integer.toString(i+1), "Jiajun", false);
newnode.setUserObject(newFolder);
root.add(newnode);
if(i < 4){
for(int j=0; j < 5; j++){
DefaultMutableTreeNode newdocNode = new DefaultMutableTreeNode();
newdocNode.setAllowsChildren(false);
DocNode newDoc = new DocNode(10*(i+1) + j, "Document" + Integer.toString(10*(i+1)+j), "Jiajun");
newdocNode.setUserObject(newDoc);
newnode.add(newdocNode);
}
}
}
回答1:
To be more helpful than in my comment, I found this code in the AbstractTreeClass which will control what image it assigns to the node:
/**
* Returns the resource reference for icon of specified tree node.
*
* @param node
* The node
* @return The package resource reference
*/
protected ResourceReference getNodeIcon(TreeNode node)
{
if (node.isLeaf() == true)
{
return getItem();
}
else
{
if (isNodeExpanded(node))
{
return getFolderOpen();
}
else
{
return getFolderClosed();
}
}
}
So, the whole thing comes to the question of what does the isLeaf() method return. I found this in the DefaultMutableTreeNode class:
public boolean isLeaf()
{
return children.size() == 0;
}
So, it seems that your combination would treat all elements without children as leafs and not as folders. Maybe you could overwrite the getNodeIcon method using the getAllowsChildren, making the necessary type adjustments...
Another idea is to overwrite the isLeaf() method of DefaultMutableTreeNode, but then you might get other unexpected issues if it is called somewhere you cannot control...
This is just some insight on how you could do it... Hope it is helpful...
回答2:
Which Wicket version are you using? The old tree implementation is deprecated in 6.x and removed in 7.x, so you should use the new implementation in package org.apache.wicket.extensions.markup.html.repeater.tree - it is no longer based on the Swing classes.
回答3:
The problem is with the line if(i < 4){
in the outer for
loop. You allow the node to have children but you don't create any children for this last node.
Edit: You are correct about the empty folder so my answer is incorrect. In Swing this works as expected. When the tree row is rendered the decision to draw a folder or file icon is based upon the flag setAsksAllowsChildren
. So the problem seems to be on Wicket's tree component
回答4:
You can override Folder.getStyleClass to return the desired Icon. I got something like this:
@Override
protected String getStyleClass() {
String styleClass;
if (isFolder(getModelObject()))
{
if (tree.getState(matter) == State.EXPANDED)
{
styleClass = getOpenStyleClass();
} else {
styleClass = getClosedStyleClass();
}
} else {
styleClass = getOtherStyleClass(matter);
}
if (isSelected()) {
styleClass += " " + getSelectedStyleClass();
}
return styleClass;
}
Your implementation of isFolder() may vary.
来源:https://stackoverflow.com/questions/17809452/defaulttreemodel-and-wicket-tree-setasksallowschildren-doesnt-work