2

I have a TabbedPanel in which I dynamically add and remove tabs. In addition I want to change the title of a tab according to its changing contents. In my current code the title is set by the Wicket ID like:

public class GenericTab extends AjaxTab {
private boolean closable = true;

public GenericTab( MyAbstractPanel myPanel ) {
    super( Model.of( myPanel.getTitle() ) );
}

So I can set the title once at instantiation. How can I change it with Java code?

Jemolah
  • 1,962
  • 3
  • 24
  • 41

2 Answers2

1
public GenericTab( MyAbstractPanel myPanel ) {
  super( new PropertyModel<String>(myPanel, "title") );
}
svenmeier
  • 5,681
  • 17
  • 22
  • How does this approach help me to modify the title later? – Jemolah Mar 21 '16 at 19:56
  • Check https://cwiki.apache.org/confluence/display/WICKET/Working+with+Wicket+models#WorkingwithWicketmodels-DynamicModels. PropertyModel is a dynamic model, so if you change MyAbstractPanel#title with your APIs then automatically the new title will be used by the tab's title. – martin-g Mar 22 '16 at 10:27
0

You have to extend an AjaxTabbedPanel and override the newTitle method like this:

            @Override
            protected Component newTitle(String titleId, IModel<?> titleModel, int index) {
                Label updatableLabel = new Label(titleId, titleModel) {
                    @Override
                    public void onEvent(IEvent<?> event) {
                        super.onEvent(event);
                        Object payload = event.getPayload();
                        if (payload instanceof MyAjaxEvent) {
                            ((MyAjaxEvent) payload).getTarget().add(this);
                        }
                    }
                };
                updatableLabel.setOutputMarkupId(true);
                return updatableLabel;
            }

After that you can update a tab title with

send(getPage(), Broadcast.BREADTH, new MyAjaxEvent(target, model));
A.Alexander
  • 588
  • 3
  • 14