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>();
Option firstOption = new Option(personDataProvider.getValue("PERSON.PERSONID"), (String) personDataProvider.getValue("PERSON.NAME"));
Option filler = new Option(99999, "----------");
while (personDataProvider.cursorNext()) {
Option option = new Option(personDataProvider.getValue("PERSON.PERSONID"), (String) personDataProvider.getValue("PERSON.NAME"));
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.


g. neidisch said...

i eventually add more than 20 options to my dropdown and it works fine: they are displayed ... with a scrollbar.
Do you know how can I get rid of that scroll bar and just show all the options?

John Yeary said...

The design of the component is to display options in a compact way. A determination was made to limit the number of items displayed, and provide a scrollable list if the limit was exceeded.

This makes sense because otherwise you could have 250 items which extend past the page boundaries. I am sure you have seen such poorly designed components where the list goes off the screen.

If you really want to change it, you must check out the code, and modify the list limit to 20-25.

Popular Posts