Skip to content

Categories:

Replacing panels within an onClick gotcha

I was trying to replace a Panel in a fairly complex page, by doing:

Panel viewer = ...
add(viewer);

(…)

public void onClick() {
Panel newDetailPanel = new DetailPanel(viewer.getId(), documentModel);
viewer.replaceWith(
newDetailPanel);
}

… that seems and works fine — but only on the first click!

The problem is: if I carry on clicking / replacing Panels, Wicket will throw an Exception:

java.lang.IllegalStateException: This method can only be called on a component that has already been added to its parent.

This happens because the second time we click on the link, Wicket loses the reference to the original viewer Panel, therefore not being able to determine whether it has been added to some component in the hierarchy (and can’t call replaceWith on a component that hasn’t been added).

So we will need to update that reference:

public void onClick() {
YourMainPanelOrPage.this.addOrReplace(viewer);
Panel newDetailPanel = new DetailPanel(viewer.getId(), documentModel);
viewer.replaceWith(
newDetailPanel);
viewer =
newDetailPanel;
}

…and now it should work.

Share and Enjoy:
  • Digg
  • Print this article!
  • del.icio.us
  • Facebook
  • Google
  • E-mail this story to a friend!
  • Slashdot

Posted in general.

0 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

Some HTML is OK

(required)

(required, but never shared)

or, reply to this post via trackback.