In the Wicket DropDownChoice how can you replace "Choose one" to another text
Asked Answered
M

5

10

I have a DropDownChoice like below:

    final DropDownChoice<Term> terms = new DropDownChoice("terms", new Model<Term>(), new Model(new ArrayList(termDao.findAll())), new IChoiceRenderer<Term>() {
        public Object getDisplayValue(Term object) {
            return object.getIdentifier();
        }

        public String getIdValue(Term object, int index) {
            return object.getId().toString();
        }
    });

I want to have "Choose All" instead of "Choose one". How can I do that?

Mongo answered 9/1, 2012 at 11:24 Comment(1)
FYI you can add that as an answer to your own question and accept it to get credit for itCham
M
6
  1. Set a markup id for your DropDownChoice.: terms.setMarkupId("termsDDC");

  2. Create a .properties file for your form/panel/page. For example: mypanel.properties

  3. In the property file write: termsDDC.null=Choose All

Ref: https://cwiki.apache.org/WICKET/dropdownchoice.html

Mongo answered 10/1, 2012 at 9:16 Comment(0)
L
9

I tried Goli's suggestion under wicket 6.4 and it doesn't work. For me the right way is:

  1. It is not necessary to set terms.setMarkupId("termsDDC"); It will work without it

  2. Exactly as above, if you have a form on the panel (wicket:id="form") and a DropDownChoice on the form (wicket:id="terms"), it doesn't matter, you should name .properties file as mypanel.properties

  3. In the property file write: form.terms.null=Choose All or form.terms.nullValid=Empty, if the dropdown has setNullValid(true)

Latakia answered 19/7, 2013 at 13:16 Comment(0)
M
6
  1. Set a markup id for your DropDownChoice.: terms.setMarkupId("termsDDC");

  2. Create a .properties file for your form/panel/page. For example: mypanel.properties

  3. In the property file write: termsDDC.null=Choose All

Ref: https://cwiki.apache.org/WICKET/dropdownchoice.html

Mongo answered 10/1, 2012 at 9:16 Comment(0)
T
6

I'm using wicket 6.14 (not sure which version it was introduced) and you can just override getNullKeyDisplayValue(), so you would have this:

final DropDownChoice<Term> terms = new DropDownChoice("terms", new Model<Term>(), new Model(new ArrayList(termDao.findAll())), new IChoiceRenderer<Term>() {
    @Override
    protected String getNullKeyDisplayValue() {
        return "Choose All";
    }

    public Object getDisplayValue(Term object) {
        return object.getIdentifier();
    }

    public String getIdValue(Term object, int index) {
        return object.getId().toString();
    }
});
Triserial answered 20/6, 2014 at 11:20 Comment(0)
M
0

I used the two methods:

AbstractSingleSelectChoice#getNullKeyDisplayValue(), AbstractSingleSelectChoice#getNullValidDisplayValue()

which are both accessed through DropDownChoice

 @Override
 protected String getNullKeyDisplayValue() {
    return "Choose All";
 }

and if the DropDownChoice has had setNullValid(true) the method:

     @Override
     protected String getNullValidDisplayValue() {
        return "Choose All";
     }
Mohawk answered 16/2, 2017 at 14:39 Comment(0)
O
0

In older Wicket versions (maybe also works in newer versions) you can do the following (tested in Wicket 1.3.7):

Dropdownchoice dropdown = new DropDownChoice("dropdown", list)
{
    @Override
    protected CharSequence getDefaultChoice(Object selected)
    {
        return "<option value=\"\">" + "MY placeholder text" + "</option>";
    }
};
dropdown.setNullValid(true);

Maybe you want to add additional stuff to the option-tag depending on the object "selected". Take a look at the super.getDefaultChoice(Object selected) method for the default implementation.

Outburst answered 2/5, 2017 at 8:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.