Calling getString () in the constructor in Wicket gives an error


I'm having some problems with localization in wicket.

This is the code:

private String displayString;
private TextField<String> myTextField;

public myPage(DomainObject domainObject){
    if(domainObject != null)
        displayString = domainObject.getDisplayString();
    myTextField = new TextField<String>("myTextField", new PropertyModel<String>(this, "displayString"));

        displayString = getString("mandatory"); //<- error message here


The problem is that calling getString in the constructor results in an error message("...This can sometimes lead to an invalid or no localized resource returned..."). I want to use a PropertyModel for the TextField since I don't want to translate the string I get from domainObject.getDisplayString(). I don't want the changes made in the TextField to affect the value in domainObject directly. It's possible to get rid of the error message by doing this instead of getString:

    displayString = new ResourceModel("mandatory").getObject(); //<- no error message

To my understanding, this is the same thing as calling getString (you just hack away the warnings, but the problem still exist). A solution i thought of is this:

protected void onAfterRender() {
        displayString = getString("mandatory"); //<-  no error message

Does anyone see a problem with this solution? Maybe I'm not thinking "wickety" enough?

Calling getString() requires the component to be inside a component hierarchy, where it can access it's parent to have the chance to fall back to properties defined there or further up in the tree. This isn't possible inside the component's constructor (as you add it to it's parent at a later point). Wicket 1.5 introduces the onInitialize function for these operations. With Wicket versions prior to this, there is an easy way to emulate this behaviour:

In your base component and page define a non-final empty method as

protected void onInitialize() {}

and add this to the onBeforeRender method:

protected void onBeforeRender() {
    if (!hasBeenRendered()) {

Then you can use an overridden onInitialize() method in any of your components to deal with stuff that has to wait until the component hierarchy is established.