Jemolah
Jemolah

Reputation: 2192

When can I use getString() in a Wicket Ajax TabbedPanel?

I have the following problem with Wicket 7.3 and JQuery 2.1.4:

In a dynamic tabbed panel (tabs are added and removed), I want to localize the tab titles and add tooltips. My code

JQueryGenericPanel() {
....
  populateItem( ListItem<ITab> item) {
    getString();

results in a warning in the log file:

Tried to retrieve a localized string for a component that has not yet been added to the page. This can sometimes lead to an invalid or no localized resource returned. Make sure you are not calling Component#getString() inside your Component's constructor

Using getString() in the panel (which is on the tab) within its method

onInitialize()

does not work, because its too late. The label is already set to "lazy".

Is there any other method similar to "populateItem()" which I can use?

** Addendum ** The code for the tabbed panel is:

public class MyTabbedPanel extends JQueryGenericPanel<List<ITab>> implements ITabsListener {
  ...
  @Override
  protected void onInitialize() {
    super.onInitialize();

    this.add( new ListView<ITab>( "tabs", this.getModel() ) {
      ...
      @Override
      protected void populateItem( ListItem<ITab> item ) {
        Label link = new Label( "widgetId", new PropertyModel<String>( somePanel, "getTitle()" ) );

The code in the panel is:

private String title = "default";
public String getTitle() { return title; }
@Override
public void onInitialize() {
    title = getString( "someKey" );
}

So the PropertyModel fetches the title with 'getTitle()'. Unfortunately this happens before 'onInitialize()' gets called. So the tab title shows "default" instead of the localized text for "someKey".

Upvotes: 1

Views: 1341

Answers (1)

svenmeier
svenmeier

Reputation: 5681

Instead of

new Label(itemId, getString("key"))

... use:

new Label(itemId, new ResourceModel("key"));

... or if you doing something fancy with the string:

new Label(itemId, new AbstractReadOnlyModel<String>() {
  public String getObject() {
    return ... + getString("key") + ...;
  }
});

Upvotes: 0

Related Questions