Class Select<T extends Select<T>>


public class Select<T extends Select<T>> extends InputElement<T>
Represents the HTML <select> element for creating dropdown selection lists.

The Select class provides a Java representation of the HTML select element, which creates a dropdown list of options from which users can choose. It supports both single and multiple selection modes, dynamic option management, and provides comprehensive methods for managing the selected state and retrieving values in various data types.

Features:

  • Dynamic option addition and removal
  • Selection by index, text, or value
  • Support for initial placeholder text
  • Type-safe value retrieval (int, long, float, double, boolean)
  • Multiple selection support
  • Selected option and options list access
  • Automatic value synchronization
  • Event-driven selection change handling
  • Reset capability to restore initial state

Usage Example:


 // Create a country selector
 Select countrySelect = new Select("country");
 countrySelect.setInitialText("-- Select Country --");
 countrySelect.addOption("US", "United States");
 countrySelect.addOption("CA", "Canada");
 countrySelect.addOption("MX", "Mexico");
 countrySelect.setSelectedValue("US");

 // Create a number selector with typed retrieval
 Select quantitySelect = new Select("quantity");
 quantitySelect.addOption(1, "One");
 quantitySelect.addOption(2, "Two");
 quantitySelect.addOption(5, "Five");
 quantitySelect.addOption(10, "Ten");
 int quantity = quantitySelect.getSelectedValueAsInt();

 // Create a select with option groups
 Select carSelect = new Select("car");
 carSelect.setInitialText("-- Choose a car --");

 OptGroup swedish = new OptGroup();
 swedish.setLabel("Swedish Cars");
 swedish.addOption(new Option("volvo", "Volvo"));
 swedish.addOption(new Option("saab", "Saab"));
 carSelect.addOptGroup(swedish);

 OptGroup german = new OptGroup();
 german.setLabel("German Cars");
 german.addOption(new Option("mercedes", "Mercedes"));
 german.addOption(new Option("audi", "Audi"));
 carSelect.addOptGroup(german);
 
Since:
2007
Version:
1.0
Author:
Marvin P. Warble Jr.
See Also:
  • Constructor Details

    • Select

      public Select()
      Creates a new empty Select element.
    • Select

      public Select(String name)
      Creates a new Select element with the specified name.
      Parameters:
      name - the form field name
  • Method Details

    • setInitialText

      public final void setInitialText(String text)
      Sets the initial placeholder text shown before a selection is made. The placeholder option is disabled and cannot be selected.
      Parameters:
      text - the placeholder text to display
    • addOption

      public final void addOption(Object value, String text)
      Adds an option with the specified value and display text.
      Parameters:
      value - the option value
      text - the text to display
    • addOption

      public final void addOption(Object value)
      Adds an option where the value is used as both value and display text.
      Parameters:
      value - the option value and display text
    • addOption

      public final void addOption(Option option)
      Adds an Option object to this select element.
      Parameters:
      option - the Option to add
    • addOptGroup

      public final void addOptGroup(OptGroup group)
      Adds an option group to this select element.

      The OptGroup and all of its contained Option elements are added to this select. Options within the group are automatically included in selection tracking, so methods like getSelectedIndex(), setSelectedValue(Object), and getSelectedOption() work across all options regardless of grouping.

      Parameters:
      group - the OptGroup to add
    • setSelectedIndex

      public final void setSelectedIndex(int index)
      Sets the selected option by index.
      Parameters:
      index - the zero-based index of the option to select
    • setSelectedText

      public final void setSelectedText(String text)
      Sets the selected option by matching display text.
      Parameters:
      text - the display text of the option to select, or null to deselect
    • setSelectedValue

      public final void setSelectedValue(Object value)
      Sets the selected option by matching value.
      Parameters:
      value - the value of the option to select, or null to deselect
    • removeAllOptions

      public final void removeAllOptions()
      Removes all options and option groups from this select element.
    • removeOption

      public final void removeOption(int index)
      Removes the option at the specified index.
      Parameters:
      index - the zero-based index of the option to remove
    • removeOptionByValue

      public final void removeOptionByValue(Object value)
      Removes the option with the specified value.
      Parameters:
      value - the value of the option to remove
    • removeOptionByText

      public final void removeOptionByText(String text)
      Removes the option with the specified display text.
      Parameters:
      text - the display text of the option to remove
    • getSelectedIndex

      public final int getSelectedIndex()
      Returns the index of the currently selected option.
      Returns:
      the zero-based selected index
    • getSelectedOption

      public final Option getSelectedOption()
      Returns the currently selected Option.
      Returns:
      the selected Option, or null if none selected
    • getSelectedOptions

      public final List<Option> getSelectedOptions()
      Returns all selected options (for multiple select).
      Returns:
      a list of selected Option objects
    • getSelectedText

      public final String getSelectedText()
      Returns the display text of the selected option.
      Returns:
      the selected text, or null if none selected
    • getSelectedValue

      public final String getSelectedValue()
      Returns the value of the selected option.
      Returns:
      the selected value, or null if none selected
    • getSelectedValueAsInt

      public final int getSelectedValueAsInt()
      Returns the selected value as an integer.
      Returns:
      the selected value parsed as int, or 0 if null
    • getSelectedValueAsLong

      public final long getSelectedValueAsLong()
      Returns the selected value as a long.
      Returns:
      the selected value parsed as long, or 0 if null
    • getSelectedValueAsShort

      public final short getSelectedValueAsShort()
      Returns the selected value as a short.
      Returns:
      the selected value parsed as short, or 0 if null
    • getSelectedValueAsFloat

      public final float getSelectedValueAsFloat()
      Returns the selected value as a float.
      Returns:
      the selected value parsed as float, or 0 if null
    • getSelectedValueAsDouble

      public final double getSelectedValueAsDouble()
      Returns the selected value as a double.
      Returns:
      the selected value parsed as double, or 0 if null
    • getSelectedValueAsBoolean

      public final boolean getSelectedValueAsBoolean()
      Returns the selected value as a boolean.
      Returns:
      the selected value parsed as boolean, or false if null
    • getValue

      public final Object getValue(int index)
      Returns the value of the option at the specified index.
      Parameters:
      index - the zero-based option index
      Returns:
      the option value
    • getNumberOfOptions

      public final int getNumberOfOptions()
      Returns the number of options in this select element.
      Returns:
      the option count
    • getOptions

      public List<Option> getOptions()
      Returns all options in this select element, including options within option groups.
      Returns:
      a list of all Option objects
    • onSelectionInitialized

      public final void onSelectionInitialized(int index, String value)
      Called when the selection is first initialized from the client.
      Parameters:
      index - the initial selected index
      value - the comma-separated list of selected values
    • onSelectionChange

      public final void onSelectionChange(int index, String value)
      Called when the selection changes from the client.
      Parameters:
      index - the new selected index
      value - the comma-separated list of selected values
    • reset

      public boolean reset()
      Resets the selection to its initial state.
      Overrides:
      reset in class InputElement<T extends Select<T>>
      Returns:
      true if the value was changed
    • onSelectedIndexSet

      protected void onSelectedIndexSet(int index)
      Called when the selected index is programmatically set. Override this method to add custom behavior.
      Parameters:
      index - the new selected index
    • onSelectedValueSet

      protected void onSelectedValueSet(Object value)
      Called when the selected value is programmatically set. Override this method to add custom behavior.
      Parameters:
      value - the new selected value