title: Updating a datatable in Wicket
body: <p>We are developing a new application in Wicket and have run into a small problem.<br></p>
  
  <p>What we do:</p>
  
  <p>1) create a new SortableDataProvider<br>
  2) create a new DefaultDataTablePagingInBottom<br>
  3) create a new WebMarkupContainer<br>
  4) add the DefaultDataTablePagingInBottom to the WebMarkupContainer<br>
  5) create a new AjaxCheckBox<br>
  6) in the onUpdate of the AjaxCheckBox, add the WebMarkupContainer to the AjaxRequestTarget<br>
  7) set the SortableDataProvider to a new SortableDataProvider (with the updated query)<br>
  8) DefaultDataTablePagingInBottom.replaceWith(new DefaultDataTablePagingInBottom - with the new provider).<br></p>
  
  <p>What happends:</p>
  
  <p>1) Click the checkbox -> nothing happends.<br>
  2) Click it again -> crash: "Last cause: This method can only be called on a component that has already been added to its parent.<br>
  WicketMessage: Method onRequest of interface org.apache.wicket.behavior.IBehaviorListener targeted at org.apache.wicket.ajax.markup.html.form.AjaxCheckBox$1@1a2fefd on component [ [Component id = checkBox]] threw an exception"<br>
  3) Click back in the browser -> the list i filtered with the new provider.</p>
  
  <p>Any ideas?</p>
  
  <p>---EDIT---
  Here's some code.</p>
  
  <p>1) In the constructor of the WebPage:</p>
  
  <pre><code>model = new Model(projectPlannerService);
  provider = new SortableProjectDataProvider(model, (WebSession) getSession(), isChecked);
  table = new DefaultDataTablePagingInBottom("table", columns, provider, 50);
  
  listContainer = new WebMarkupContainer("wmc");
  
  listContainer.add(table);
  add(listContainer.setOutputMarkupId(true));
  
  /*
   * checkbox för filtrering
   */
  AjaxCheckBox checkBox = new AjaxCheckBox("checkBox", new Model()) {
  
      @Override
      protected void onUpdate(AjaxRequestTarget target) {
          target.add(listContainer, "wmc");
          isChecked = !isChecked;
          provider = new SortableProjectDataProvider(model, (WebSession) getSession(), isChecked);
          updateTable();
      }
  };
  add(checkBox);
  </code></pre>
  
  <p>2) In updateTable():</p>
  
  <pre><code>table.replaceWith(new DefaultDataTablePagingInBottom("table", columns, provider, 50));
  </code></pre>
  
  <p>3) The SortableProjectDataProvider:</p>
  
  <pre><code>// Constructor
  public SortableProjectDataProvider(IModel&lt;?&gt; model, WebSession webSession, boolean isChecked) {
      this.model = model;
      this.projectPlannerService = (ProjectPlannerService) model.getObject();
      this.webSession = webSession;
      setSort("customer", SortOrder.ASCENDING);
      System.out.println("ischecked:" + isChecked);
      if(!isChecked)
          list = ((ProjectPlannerService) model.getObject()).findAllProjects();
      else
          list = ((ProjectPlannerService) model.getObject()).findAllActiveProjects();
  
      System.out.println("size: " + list.size());
  
      comparator = new ProjectComparator();
  }
  
  public Iterator&lt;Project&gt; iterator(int first, int count) {
  
      Collections.sort(list, comparator);
      if (first &gt; list.size()) {
          first = 0;
      }
      if (first + count &gt; list.size()) {
          return list.subList(first, list.size()).iterator();
      } else {
          return list.subList(first, first + count).iterator();
      }
  }
  
  public IModel&lt;Project&gt; model(Project object) {
      return new DetachableProjectModel((Project) object);
  }
  
  public int size() {
      return list.size();
  }
  
  
  private class DetachableProjectModel extends LoadableDetachableModel {
  
      private Long id;
      @SpringBean
      ProjectPlannerService projectPlannerService;
  
      public DetachableProjectModel(Long id) {
          Injector.get().inject(this);
          if (id == null) {
              throw new IllegalArgumentException();
          }
          this.id = id;
      }
  
      public DetachableProjectModel(Project project) {
  
          this(project.getPk());
          Injector.get().inject(this);
      }
  
  
      public int hashCode() {
          return id.hashCode();
      }
  
  
      public boolean equals(final Object obj) {
          if (obj == this) {
              return true;
          } else if (obj == null) {
              return false;
          } else if (obj instanceof DetachableProjectModel) {
              DetachableProjectModel other = (DetachableProjectModel) obj;
              return other.id == this.id;
          }
          return false;
      }
  
  
      protected Object load() {
          return ((ProjectPlannerService) model.getObject()).findProjectById(id);
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>wicket:extend<br>
     -input wicket:id="checkBox" type="checkbox"- Show active -/input-<br>
     -div wicket:id="wmc"-<br>
     -table wicket:id="table"--/table-<br>
     -/div-<br>
  -/wicket:extend-<br></p>
  
  <p>Thanks in advance!<br>
  /Andreas</p>