Question

Im having some problem with DropDownChoice. I have an Enum with a list of school title like:

public enum StudyTitle {

    NONE(null,null),ELEMENTARY("1","Elementary"),COLLEGE("2","College");

    private String code;
    private String description;

    private StudyTitle(String code, String description){
        setCode(code);
        setDescription(description);
    }

    [setter and getter]

}

Then I have a Pojo with a String proprerty call "studyTitleCode" where I want to put the code (ex 1 for elementary, 2 for college etc...).

When I create a DropDownChoice Wicket doesn't allow me to have a proprerty Model of type String if the DropDownChoice is of type StudyTitle.

Ex. [building the listOfStudyTitle as ArrayList of Enums]

DropDownChoice<String> studyLevel = new DropDownChoice<String>("id",new PropertyModel<String>(myPojo,"studyTitleCode"),listOfStudyTitle,new ChoiceRenderer<StudyTitle>("description","code"));

Is there a Method to allow Wicket to link one property of the Enum to the Property of Model?

Thanks

Was it helpful?

Solution

The choice options for an AbstractSingleSelectChoice must match the type of the value model. The only related config option for the DropDownChoice that I'm aware of is the IChoiceRenderer which allows you to set how the enum value is rendered (vs the default call toString()).

One option would be, instead of using the enum instance itself for your choices model, give the enum a String property that can be used:

public enum TestEnum {
    ONE ("ONE"),
    TWO ("TWO"), 
    THREE ("THREE");

    private String value;

    TestEnum(String value) {
        this.value = value;
    }

    public String getValue() {
        return value;
    }

    public static List<String> getStringValues()
    {
        List<String> stringValues = new ArrayList<String>();
        for (TestEnum test : values()) {
            stringValues.add(test.getValue());
        }

        return stringValues;
    }
}

@Override
protected void onInitialize() {
    super.onInitialize();

    IModel<String> myStringValueModel = new Model<String>();
    add(new DropDownChoice<String>("id", myStringValueModel, TestEnum.getStringValues()));
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top