Saturday, July 04, 2009

Woodstock Dropdown List Binding Examples

I am spending the morning answering a number of questions posted to the nbj2ee list. One of the questions was how to add separators to to drop down lists. I decided to write a little code example to show how this is done.

The idea is to retrieve the data values from a database. In this case, I use a built in travel database from NetBeans.

There are two examples. The first one is a simple database binding, and the second uses the Option class to control the output.

The Option class allows us to insert any "filler" into the items we display in the list. Here is the code.
    public Option[] getOptions() {
List<Option> options = new ArrayList<Option>();
personDataProvider.cursorFirst();
Option firstOption = new Option(personDataProvider.getValue("PERSON.PERSONID"), (String) personDataProvider.getValue("PERSON.NAME"));
options.add(firstOption);
Option filler = new Option(99999, "----------");
options.add(filler);
while (personDataProvider.cursorNext()) {
Option option = new Option(personDataProvider.getValue("PERSON.PERSONID"), (String) personDataProvider.getValue("PERSON.NAME"));
options.add(option);
}
return options.toArray(new Option[options.size()]);
}
Here is the source code DropDownListExample. The example was created using NetBeans 6.5.1 and Visual JSF plugin.

Popular Posts