how to get the display name of the dropdownlist?

How can i get the display name of the dropdownlist.I want to have value as other data not the same as display name.In that case is there any easy way to get the display name of the selection from the dropdownlist.
[234 byte] By [kirandeluxea] at [2007-11-26 23:41:22]
# 1

You mean the option label? If you're using SelectItem, then you can specify the String label as 2nd parameter.

SelectItem selectItem = new SelectItem(someObject, "The label to be shown in menu");

Otherwise by default the value of someObject.toString() will be used as label.

You also can override it by

public class SomeObject {

public String toString() {

return this.name; // Change the String representation of your object here.

}

}

and then stay using

SelectItem selectItem = new SelectItem(someObject);

Also see the [url=http://java.sun.com/javaee/javaserverfaces/1.2/docs/api/javax/faces/model/SelectItem.html]SelectItem API[/url].

BalusCa at 2007-7-11 15:08:53 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

I usually just use a hashmap to store the value and label, if I want to retrieve the label again later.

HashMap bleh = new HashMap();

bleh.put(dropDownValue, "Drop Down Label"); //Item value (any object), Item Label (String)

Iterator it = bleh.keySet().iterator();

while (it.hasNext()) {

Object key = it.next();

SelectItem selectItem = new SelectItem(key, bleh.get(key));

}

//And then later, when you want to retrieve the label

bleh.get(itemValue); //The item value of the label you want

The value is easy to get because the selected item(s) from the list will be set to the value(s) the user selected. You should have a getter in your bean for that.

CowKing

Message was edited by:

IamCowKing

IamCowKinga at 2007-7-11 15:08:54 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3
How are you ordering the selectitem list? HashMap doesn't maintain the order. I'd rather to use a LinkedHashMap which maintains the order thanks to a backing linked list.
BalusCa at 2007-7-11 15:08:54 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4
Ordering has never been important to me yet. But if it were, that sounds like a perfectly reasonable way to maintain order.CowKing
IamCowKinga at 2007-7-11 15:08:54 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...