Java tutorial
//package com.java2s; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.Hashtable; import java.util.Vector; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; public class Main { protected static Log log = LogFactory.getLog("COLLECTION_UTILITIES"); /** * Method getDropDownOptions. * * @param vector Vector * @param valueGetter String * @param textGetter String * @return String * @throws NoSuchMethodException * @throws InvocationTargetException * @throws IllegalAccessException */ public static String getDropDownOptions(Vector vector, String valueGetter, String textGetter) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { log.debug("Scmportal: getDropDownOptions(Vector, String, String) called with parameters " + vector + ", " + valueGetter + ", and " + textGetter + " as parameters"); return getDropDownOptions(vector, valueGetter, textGetter, ""); } /** * Method getDropDownOptions. * * @param vector Vector * @param valueGetter String * @param textGetter String * @param selectedValue Object * @return String * @throws NoSuchMethodException * @throws IllegalAccessException * @throws InvocationTargetException */ public static String getDropDownOptions(Vector vector, String valueGetter, String textGetter, Object selectedValue) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { Vector selectedValues = new Vector(); if (selectedValue != null) { selectedValues.add(selectedValue); } return getDropDownOptions(vector, valueGetter, textGetter, selectedValues); } /** * Method getDropDownOptions. * * @param vector Vector * @param valueGetter String * @param textGetter String * @param selectedValues Vector * @return String * @throws NoSuchMethodException * @throws IllegalAccessException * @throws InvocationTargetException */ public static String getDropDownOptions(Vector vector, String valueGetter, String textGetter, Vector selectedValues) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { Hashtable selectedValuesHashtable = new Hashtable(selectedValues.size()); for (int i = 0; i < selectedValues.size(); i++) { selectedValuesHashtable.put(selectedValues.elementAt(i).toString(), ""); } StringBuffer dropDownOptions = new StringBuffer(""); String optionValue = null; String optionText = null; Class[] clsParms = new Class[0]; Object[] objParms = new Object[0]; Method getterMethod = null; Object vectorElement = null; for (int i = 0; i < vector.size(); i++) { vectorElement = vector.elementAt(i); getterMethod = vectorElement.getClass().getMethod(valueGetter, clsParms); optionValue = getterMethod.invoke(vectorElement, objParms).toString(); getterMethod = vectorElement.getClass().getMethod(textGetter, clsParms); optionText = getterMethod.invoke(vectorElement, objParms).toString(); dropDownOptions.append("<option value=\"" + optionValue + "\""); if (selectedValuesHashtable.containsKey(optionValue)) { dropDownOptions.append(" selected"); } dropDownOptions.append(">" + optionText); } return dropDownOptions.toString(); } }