Wicket Gems

it happens quite often that, while browsing the Wicket mailing list, i stumble upon gems, that i fail to find or even remember when i could use them.
this really has to stop

the problem is: there is of course no commen theme about these hacks, components, behaviours etc, so it is very hard to find a good name for a project like that.
i thought of wicket-contrib-kitchensink, but maybe i should give it another try.

until then, i’ll grab these gems and republish them here (after maybe adding a few things and polishing a little) just to make sure, that i wont forget them.
here is the first:

How would one implement “hint text” on a text field in Wicket? By hint text
I mean an empty TextField has greyed out text in it to indicate its purpose,
and gets cleared onFocus?)

the answer & code is from mighty igor vaynberg:

 Java |  copy |? 
                    
/**
 * Adds hint-text to texfield, that will be erased onFocus
 * @author igor vaynberg
 */
public class TextFieldHintBehaviour extends AbstractBehavior
{
    private String pseudoUniqueJavascriptVariableName = this.getClass().getSimpleName()
            + String.valueOf(System.nanoTime());
 
    public TextFieldHintBehaviour(final IModel hintTextModel)
    {
        this.hint = hintTextModel;
    }
 
    private final IModel hint;
    private Component c;
 
    private String hintColor = "gray";
    private String textColor = "black";
 
    @Override
    public void bind(final Component component)
    {
        super.bind(component);
        this.c = component;
        this.c.setOutputMarkupId(true);
    }
 
    @Override
    public void renderHead(final IHeaderResponse response)
    {
        response.renderOnDomReadyJavascript("var " + this.pseudoUniqueJavascriptVariableName
                + "=document.getElementById('" + this.c.getMarkupId() + "');" + this.pseudoUniqueJavascriptVariableName
                + ".value='" + this.hint.getObject() + "';" + this.pseudoUniqueJavascriptVariableName
                + ".style['color']='" + this.hintColor + "';");
    }
 
    @Override
    public void onComponentTag(final Component component, final ComponentTag tag)
    {
        super.onComponentTag(component, tag);
        tag.put("onfocus", "if (this.value=='" + this.hint.getObject() + "') {this.value=''; this.style['color']='"
                + this.textColor + ";'}");
    }
 
    public String getHintColor()
    {
        return this.hintColor;
    }
 
    public void setHintColor(final String hintColor)
    {
        this.hintColor = hintColor;
    }
 
    public String getTextColor()
    {
        return this.textColor;
    }
 
    public void setTextColor(final String textColor)
    {
        this.textColor = textColor;
    }
}

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>