Oracle Blog

Blog for david

Working With Custom Objects as Options in a Drop Down List

I recently got involved helping a user with a problem concerning a “Drop Down List” component and a custom “OptionsList”. Vana, “yovana” on the Creator Forum created a custom “OptionsList” but had the problem where the selected item would not show up. I came up with a simplified example that showed the same problem and though I'd share the problem and solution with everyone knowing that sooner or later, someone else would stumble on this issue.

Problem Statement

You have a drop down list that you want to show items that don't come from a Creator data source. The items are of a custom type. When you select one of these items, you expect the selected item to show selected when it is selected in the drop down list.

Solution

First, lets start by setting up the web application. We do this by dropping a “Drop Down List” component on page1.

Next, we add an property to the session bean by right-clicking on “SessionBean1. Java” in the project navigator and choosing “add property”. In the property dialog, we set “name” to “options” and “Type” to “OptionsList”. Then we edit “SessionBean1.java” and add the following code to the constructor.

The “Drop Down List” plumbing will try to set the selected by comparing a “String” value to each item in the list. This means if your custom object is not of type “String” you MUST override “equals”.

Since the “Drop Down List” plumbing only can use values in the list that are rendered for determining what's been selected, the values will always be the “toString” value of the custom object since the actual Object instance can not be used in a rendered HTML page by the browser. This means the “compareTo” value passed to “equals” will be the “toString” equivelant.

Lastly, we need to bind the “Drop Down List” to “sessionBean1” “options”. We do this by selecting the “Drop Down List” on design view of “page1”, right-clicking and selecting “Property Bindings...”. The following figure shows the dialog that will appear. We then select SessionBean1->options=>options.

I hope this helps those of you hoping do make custom OptionsLists and custom Object types for those lists.