0

html file which looks like this:

<wicket:panel>
    <form wicket:id="adminUIForm">
        <table wicket:id="datatable" class="datatable"></table>
        <div wicket:id="institutTablePanel"></div>
    </form>
</wicket:panel>

But in my Java Code I only want to instantiate the component institutTablePanel, when a row in the datatable is clicked, because the constructor looks like this:

target.add(new InstitutTablePanel("institutLabelPabel", selectedContact));

And selectedContact cannot be empty. Is there a possibility of doing this without getting an error that it cannot find the component in my JavaCode?

Edit: This might be helpful

@Override
protected Item<Kontakt> newRowItem(final String id, final int index,
    final IModel<Kontakt> model) {
        Item<Kontakt> rowItem = new Item<Kontakt>(id, index, model);
        rowItem.add(new AjaxEventBehavior("onclick") {

        private static final long serialVersionUID = 1L;

        @Override
        protected void onEvent(AjaxRequestTarget target) {
             selectedKontakt = model.getObject();
             target.add(new InstitutTablePanel("institutTablePanel", selectedKontakt));
        }
    });
    return rowItem;
}

2 Answers 2

2

Create an empty WebMarkupContainer that is a blank div and add it to your page/parent component. Next, onClick, use addOrReplace and replace the the WebMarkupContainer with the InstitutTablePanel. Both the WebMarkupContainer and the InstitutTablePanel should have the same wicket:id

1
  • This is the recommended way!
    – martin-g
    Aug 29, 2015 at 6:18
1

You have to target existing components in your ajax calls, therefore i recommend initializing an InstitutTablePanel on initialization of the page with the model as Panel model. Passing model objects without the wrapping model is discouraged in wicket and leads to unnecessary problems.

You can change the InstitutTablePanel to be empty when its model object is null or empty.

Also, Wicket is built around passing models to components and not plain objects which might seem unnecessary at first but when complexity rises the pattern is preventing many bugs and errors.

Edit: dont forget to use setoutputmarkupid(true) on the added panel.

5
  • But something like: InstitutTablePanel panel = new Panel("institutTablePanel"); Can't work because Panel is abstract. The other way around I face the same problem...
    – Peter
    Aug 28, 2015 at 11:18
  • You have to create an InstitutTablePanel extending Panel first.
    – thg
    Aug 28, 2015 at 11:20
  • InstitutTablePanel does extend Panel
    – Peter
    Aug 28, 2015 at 11:25
  • What does it extend? You need to initialize your page with an instance of InstitutTablePanel and target that instance when your model object changes.
    – thg
    Aug 28, 2015 at 11:35
  • It does not help that much because I can't have an empty selectedContact... But I could initialize a Panel, thank you for that so far
    – Peter
    Aug 28, 2015 at 12:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.