anarhikos
anarhikos

Reputation: 1455

Setting default model object in a dropdownChoice?

I've a listView to list a table from the database, and I've a column to edit a row. Edit link sets the response an edit page. Edit page's constructor takes an object related with database table. I've a dropdownchoice in an edit page. And i want to initialize the dropdownchoice's selected value with objects actual values instead of "Choose One" option. groupDropDownChoice.setModelObject(user.getGroupId()); does not handle the problem. I've tried user.getGroupName(), and just user object none of them working..What to do ? thanks

public editUserPage(final User user) {
        super();
        try {
            DatabaseApp db = new DatabaseApp();
            groupTypes = db.getGroups();
            hospitals = db.getHospitals();
            polikliniks = db.getPolikliniks();
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(editUserPage.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(editUserPage.class.getName()).log(Level.SEVERE, null, ex);
        }

        addUserForm = new Form("form");
        groupDropDownChoice = new DropDownChoice("type", new Model(""), groupTypes,
                                                 new IChoiceRenderer() {

            public Object getDisplayValue(Object object) {
              return ((Group) object).getName();
            }
            public String getIdValue(Object object, int index) {
                return Integer.toString(index);
            }
        });
        groupDropDownChoice.setModelObject(user.getGroupId());

Upvotes: 1

Views: 2528

Answers (2)

zeratul021
zeratul021

Reputation: 2694

This isn't directly the answer but may be the reason why you did what you did - you tried to set model object as group id whereas you constructed the DropDownChoice from group object (not integers). So if the posted code is your actual code and you aren't stuck with wicket 1.3x or below, I would recommend you to start using generic models (Wicket 1.4x onwards). Less casting, type-safety thus what you did could not happen.

Upvotes: 0

Jawher
Jawher

Reputation: 7097

Supposing you have a method in db that returns a group given its id, this should work:

groupDropDownChoice = new DropDownChoice("type", new Model(db.getGroupById(user.getGroupId())), groupTypes, ...

The last line (where setModelObject is called) in your code snippet is redundant as the model object can be set in the component's constructor.

Upvotes: 2

Related Questions