Jemolah
Jemolah

Reputation: 2192

How do I change the title of a tab in a tabbedpanel in Apache Wicket?

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?

Upvotes: 2

Views: 728

Answers (2)

A.Alexander
A.Alexander

Reputation: 598

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

Upvotes: 0

svenmeier
svenmeier

Reputation: 5681

public GenericTab( MyAbstractPanel myPanel ) {
  super( new PropertyModel<String>(myPanel, "title") );
}

Upvotes: 1

Related Questions