You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by Christian Helmbold <ch...@yahoo.de> on 2009/04/01 14:58:02 UTC

How to switch Panels with a Tree?

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: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: How to switch Panels with a Tree?

Posted by Matej Knopp <ma...@gmail.com>.
Hi,

>  selectedPanel.replaceWith(currentPanel);

> 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)

Obviously your selected panel is not added to page and you try to call
replaceWith on it.

-Matej

On Wed, Apr 1, 2009 at 2:58 PM, Christian Helmbold
<ch...@yahoo.de> wrote:
>
> 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: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


AW: How to switch Panels with a Tree?

Posted by Christian Helmbold <ch...@yahoo.de>.
Thank you, Igor. selectedPanel=currentPanel was the solution to my issue.

Regards
Christian



      


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: How to switch Panels with a Tree?

Posted by Igor Vaynberg <ig...@gmail.com>.
selectedPanel.replaceWith(currentPanel);
selectedPanel=currentPanel;

-igor

On Wed, Apr 1, 2009 at 5:58 AM, Christian Helmbold
<ch...@yahoo.de> wrote:
>
> 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: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org