mico
mico

Reputation: 12748

Wicket: Get DropDownChoice list item names from class variable names

I am developing a search functionality with wicket. The DropDownChoice class is used to select which field of the searched item is to be searched. How to accomplish this so that the drop down names would be taken directly from the searched class.

Let's say I have a class FooBar containing variables foo and bar. If I want to select which one is the select criteria field, how I can get the naming from a resource file giving values in my resource file like

   FooBar.foo="Search for foo"
   FooBar.bar="Search for bar"

The resource file should be like that, but how to wire this in java class side?

Upvotes: 1

Views: 280

Answers (1)

bhdrkn
bhdrkn

Reputation: 6702

First of all you can get fields and its values like this

import java.lang.reflect.Field;

public class FooBar extends AbstractFooBar{
public String foo = "Search for foo";
public String bar = "Search for bar";

public static void main(String[] args) throws IllegalArgumentException, IllegalAccessException {
    Field[] fields = FooBar.class.getFields();
    // Field[] fields = AbstractFooBar.class.getFields();
    for (Field field : fields) {
        System.out.println(field.get(new Object()).toString());
    }
}

} But as you can guess you cannot use any abstraction. Also all your fields must be public.

You can initialize your class by using resource file. You can pass fields by using IChoiceRenderer, to DropDownChoice after getting field with reflection.

Your DropDownChoice type can be Field and you can initialize it by giving field list. Then in your IChoiceRenderer you can get model object.

I hope it will help.

Upvotes: 1

Related Questions