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.

    /\*\*
     \* 

Construct a new session data bean instance.

\*/ public SessionBean1() { this.options = new MyOptionsList(); }

The class “MyOptionsList” looks like this:

package dropdownselect;

import com.sun.rave.web.ui.model.Option;
import com.sun.rave.web.ui.model.OptionsList;

/\*\*
 \*
 \* @author David Botterill
 \*/
public class MyOptionsList extends OptionsList {
    
    Option [] options = new Option[2];
    
    /\*\* Creates a new instance of MyOptionsList \*/
    public MyOptionsList() {
        Object [] values = new Object[2];
        values[0] = "ID1";
        values[1] = "David";
        options[0] = new Option(new MyObject(values),"ID1");
        values = new Object[2];
        values[0] = "ID2";
        values[1] = "Jim";
        options[1] = new Option(new MyObject(values),"ID2");
        this.setOptions(options);
    }  
}

If you're copy/pasting this code, be sure to change the package name accordingly.

Next, and most importantly, we need to define “MyObject”. Here is that class:

package dropdownselect;

/\*\*
 \*
 \* @author David Botterill
 \*/
public class MyObject {
    
    Object [] objArray;
    
    /\*\* Creates a new instance of MyObject \*/
    public MyObject(Object [] inArray) {
        objArray = inArray;
    }
    
    public String toString() {
        String returnString = "";
        for(int ii=0; null != objArray && ii < objArray.length; ii++) {
            returnString += String.valueOf(objArray[ii]) + "\\n";
        }
        return returnString;
    }
    
    public boolean equals(Object compareTo) {
        String thisString = this.toString();    
        String compareToString = compareTo.toString(); 
        if(thisString.equals(compareToString)) {
            return true;
        } else return false;
    }  
}

Two very important things to remember about “MyObject”.

  1. 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”.
  2. 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.

Cheers!
-David

Comments:

Post a Comment:
Comments are closed for this entry.
About

David Botterill

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today