Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I change link-text in Wicket?

I created a link with static text. That works fine but now I also want to change the text when you click the link.

I got as far as this:

add(new Link("doAnything") {
    @Override
    public void onClick() {
            // change the text!
            // this.modelChanging();
            // this.detach();
    }
});

I hope you can give me some easy advice - I'm really new to this :)

Best regards Elias

like image 672
Sylvus Avatar asked Jul 14 '10 07:07

Sylvus


3 Answers

use the HTML tag as follow for your link and set the text in the property file for key in order to use it.

HTML:

<a wicket:id="doAnything"><wicket:message key="label.LABLENAME" /></a>

Property:

label.LABLENAME        =YOUR OWN TEXT FOR LABLE!

Java:

add(new BookmarkablePageLink<YOURCLASS>(
            "doAnything", YOURCLASS.class)));

Output would be a link label with the text specified in the property file. Simply change the corresponding text in properties to whatever text you want.

like image 107
Ehsan Amiryousefi Avatar answered Dec 11 '22 13:12

Ehsan Amiryousefi


The HTML:

<html><head></head><body>
<wicket:panel>

    <a href="" wicket:id="link">
        <wicket:container wicket:id="label" />
    </a>

</wicket:panel>
</body></html>

The java:

public class MyPanel extends Panel{

    private static class Mybean{

        String labelText = "click me";

        public String getLabelText(){
            return this.labelText;
        }

        public void setLabelText(final String labelText){
            this.labelText = labelText;
        }

    }

    public MyPanel(final String id){
        super(id);
        final Mybean bean = new Mybean();
        this.add(new Link<Void>("link"){

            private static final long serialVersionUID = 1L;

            @Override
            public void onClick(){
                bean.setLabelText("Thanks for clicking");
            }
        }.add(new Label("label", new PropertyModel<String>(bean, "labelText")))

        );

    }

}

I tend to use wicket:container in order to not pollute the HTML with superfluous elements (the wicket:container won't be rendered in production)

like image 25
Sean Patrick Floyd Avatar answered Dec 11 '22 13:12

Sean Patrick Floyd


you need to back the text in the link with its own model:

<a wicket:id="doAnything"> <span wicket:id="linktext"/> </a>

and in java:

add(new Link("doAnything").add(new Label("linktext", Model.of("i 'm the text"));

better yet if you use a (Compound)PropertyModel and have a getLinktext() function the returns the text, depending on the state.

like image 31
bert Avatar answered Dec 11 '22 14:12

bert