如何在Wicket中创建下拉列表?

时间:2010-11-06 22:50:32

标签: java drop-down-menu wicket

我正在尝试在下拉列表中填充项目。

我已经从我的数据库中获取了值,但我无法将它们添加到下拉列表中。

public List<String> getNames(){
        List<String> polNames = new LinkedList<String>();
                try {
            String query ="SELECT names FROM clinics";
            Statement statement = this.connection.createStatement();
            ResultSet result = statement.executeQuery(query);
            while(result.next()){
                polNames.add(result.getString("name"));
            }
        }
        catch(SQLException ex){
            throw new UnsupportedOperationException(ex.getMessage());
        }
        return polNames;
    }

上面,我将值分配给polNames。我希望将polNames添加到下拉列表中。

此外,单击列表中的值时,我想要执行新查询。我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:1)

要创建列表,您可以使用the DropDownChoice class。以下是它的外观示例:

DropDownChoice<String> clinicNames =
    new DropDownChoice<String>("clinicNames",
                               new PropertyModel<String>(this, "selectedName"),
                               polNames) {
        // @Override
        // public void onSelectionChanged() {
        //     // Generate and submit your query here
        // }
    };

// EDIT: onSelectionChanged() is a final method; use this instead
OnChangeAjaxBehavior clinicNamesListener = new OnChangeAjaxBehavior() {
    protected void onUpdate(AjaxRequestTarget target) {
        // Generate and submit your query here
    }
};
clinicNames.add(clinicNamesListener);

这会生成一个新的下拉列表,其中Wicket ID为“clinicNames”,填充了polNames中的值,用于更新代码中的selectedName值。