user10376560
user10376560

Reputation:

Wicket DropDownChoice selected value

I am trying to get the Id of my selected choice on a DropDownChoice but I get an error.. I know that when I choose a value I just update the model and not the object (reflection). I expected to get all the values of object "User" through getModelObject() but all i get is a NullPointerException.. I have tried many things according to tutorials and Wicket 8 documentation but nothing seems to work..

My code is like:

  // POJO
    class User {
    private Integer id;
    private String name;
    [...]

    }

    // Main.class
    private User selected;

    ChoiceRenderer<User> choiceRenderer = new ChoiceRenderer<User>("id", "name");
    List<User> list = getUsers();

    final DropDownChoice<User> dropdown1 = new DropDownChoice<User>("dropdown",
                    new PropertyModel<User>(this, "selected"), list, choiceRenderer);


    Button btn = new Button("btn") {

                private static final long serialVersionUID = 1L;

                @Override
                public void onSubmit() {
                    RecrRemoteOperations recr = new RecrRemoteOperations();
                    try {

    // NullPointerException!
    // Integer id = dropdown.getModel().getObject().getId();
      // id: the id of the selected "User" value on dropdown
                        recr.updateCommand(id);

                    } catch (Throwable e) {
                        e.printStackTrace();
                    }
                }
            }.setDefaultFormProcessing(false);

    private static List<User> getUsers() {
    List<User> allUsers = new ArrayList<User>();
    [...]
    return list;
    }

Upvotes: 1

Views: 469

Answers (2)

martin-g
martin-g

Reputation: 17503

The problem is in button.setDefaultFormProcessing(false). This tells Wicket to not use the submitted values and to not update the models of the FormComponents, i.e. the DropDownChoice won't have model object and thus won't set selected.

.setDefaultFormProcessing(false) is usually used for Cancel buttons, where you just want to leave the form.

Upvotes: 1

user10376560
user10376560

Reputation:

I am not sure but my problem is very similar to this question I was told that I don't need to use Ajax but I will try to see if it works

Upvotes: 0

Related Questions