Wicket - DropDownChoice with object Selected
Asked Answered
G

2

5

Im having problem with a DropDownChoice. I have to pre-selected an item but every tutorial and example I have found, only consider a list of primitive type.

I have a list of Object.

class myObject {
   private String name;
   private String surname;
   [setter and getter]
} 

In other class

List<MyObject> myList = some_data_retrieve();
MyObject defaultValue = some_simple_data_retrieve();

To build the DropDownChoice im using the following constuctor:

final DropDownChoice<T> ddc = new DropDownChoice<T>(id, data, new ChoiceRenderer<T>(choiceRendererExpression, choiceRendererIdExpression));

In this way:

final DropDownChoice<myObject> ddc = new DropDownChoice<myObject>("wicket_id", myList, new ChoiceRenderer<myObject>("name", "surname"));

Now. In every tutorial/example they use another constructor with a Model. For example:

private static final List<String> SEARCH_ENGINES = Arrays.asList(new String[] {
        "Google", "Bing", "Baidu" });
private String selected = "Google";
DropDownChoice<String> listSites = new DropDownChoice<String>(
        "sites", new PropertyModel<String>(this, "selected"), SEARCH_ENGINES);

I have tried something like this to emulate that kind of call:

final DropDownChoice<myObject> ddc = new DropDownChoice<myObject>("wicket_id", new PropertyModel<myObject>(this,"defaultValue"),myList, new ChoiceRenderer<myObject>("name", "surname"));

But what I got is an error:

No get method defined for class: package$WicketPage expression: defaultValue

Please, help me undersand.

Thanks

Guenna answered 27/4, 2012 at 14:1 Comment(0)
I
8

This means that you need to add a getter and setter of your "defaultValue" in the page or component where you are adding the DropDownChoice.

public class MyPage extends WebPage {

    private MyObject defaultValue;

    public MyPage(PageParameters pageParameters) {
        super(pageParameters);

        defaultValue = some_simple_data_retrieve();
        List<MyObject> myList = some_data_retrieve();

        add(new DropDownChoice<myObject>(
                       "wicket_id",
                       new PropertyModel<MyObject>(this,"defaultValue"),
                       myList, 
                       new ChoiceRenderer<MyObject>("name", "surname")
        );           
    }

    public MyObject getDefaultValue() {
        return defaultValue;
    }

    public void setDefaultValue(MyObject defaultValue) {
        this.defaultValue = defaultValue;
    }
}
Inalterable answered 27/4, 2012 at 14:25 Comment(1)
second param in constructor is model of selected element. not a model of default value. this is, how i understand wicket.Cuthburt
C
0

PropertyModel is good choice for such problems. MyObject is an object and has a string name.I have override the toString() method in it to name and it is working properly.I suggest using this method.

topicDropDown = new DropDownChoice<MyObject>("wicktID", new PropertyModel<MyObject>       (this.object, "exp"), new LoadableDetachableModel<List<MyObject>>() {
        @Override
        protected List<MyObject> load() {
            return top.getAllObjects();

        }
Cuticula answered 27/3, 2013 at 9:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.