Hello,
I want to switch panels with a tree (like switching panels within a
TabbedPanel), but get an error when clicking more than once on a node.
It should look like this:
+---------------------------------------------+
| (TreePanel) |
| +-------------+ |
| | (Tree) | |
| * root | +-----------------------+ |
| | ** Folder A | | (ContentPanel) | |
| | *** [File1] | | File1 | |
| | *** File2 | | | |
| +-------------+ +-----------------------+ |
+---------------------------------------------+
The Panel according to the selected node (File1) is displayed.
The top-level TreePanel contains the Tree and the and the ContentPanel.
Java code of TreePanel:
--------------------------------------------------------------------------------
public class TreePanel extends Panel{
private Tree tree;
private NodePanel selectedPanel;
public TreePanel(String id, NodePanel rootNode)
{
super(id);
DefaultTreeModel treeModel = new
DefaultTreeModel(rootNode.getAsTreeNode());
selectedPanel = rootNode;
add(selectedPanel);
tree = new Tree("tree", treeModel)
{
@Override
protected String renderNode(TreeNode node)
{
DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode)node;
Object userObject = treeNode.getUserObject();
if (userObject instanceof List)
return "<subtree>";
else
{
NodePanel panel = (NodePanel)userObject;
return panel.getTitle();
}
}
@Override
protected void onNodeLinkClicked(AjaxRequestTarget target,
javax.swing.tree.TreeNode node)
{
DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode)node;
NodePanel currentPanel = (NodePanel)treeNode.getUserObject();
selectedPanel.replaceWith(currentPanel);
}
};
add(tree);
}
/**
* Get a reference to the tree to configure or query it.
* @return
*/
public Tree getTree()
{
return tree;
}
}
--------------------------------------------------------------------------------
In the Page class that uses my TreePanel I add some NodePanls to the tree. When
I open the Page in the browser, the root panel is displayed as expected. When I
click on a node the first time everything is fine and the panel gets replaced,
but when I click on another link I get:
WicketMessage: Method onLinkClicked of interface
org.apache.wicket.markup.html.link.ILinkListener targeted at component
[MarkupContainer [Component id = nodeLink]] threw an exception
Root cause:
java.lang.IllegalStateException: This method can only be called on a component
that has already been added to its parent.
at org.apache.wicket.Component.replaceWith(Component.java:2717)
at
com.helmbold.wicket.components.tree.TreePanel$1.onNodeLinkClicked(TreePanel.java:53)
...
What's wrong with my code?
Regards
Christian
PS:
For completeness the code of NodePanel (ContentPanel in the example extends
NodePanel):
--------------------------------------------------------------------------------
public abstract class NodePanel extends Panel{
private DefaultMutableTreeNode treeNode = new DefaultMutableTreeNode(this);
/**
* Constructs a NodePanel with the given id.
* @param id wicket:id
*/
public NodePanel(String id)
{
super(id);
}
/**
* Wraps this object in a DefaultMutableTreeNode.
* @return A DefaultMutableTreeNode object which returns this object when
* DefaultMutableTreeNode#getUserObject() is invoked.
*/
public DefaultMutableTreeNode getAsTreeNode()
{
return treeNode;
}
/**
* Add a child in the tree hierarchy - not a nested panel this a panel!
* @param child
*/
public void addChild(NodePanel child)
{
treeNode.add(child.getAsTreeNode());
}
/**
* @return Title of the Panel that will be displayed as node link in the
tree.
*/
public abstract String getTitle();
}
--------------------------------------------------------------------------------
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]