Class Option<T extends Option<T>>


public class Option<T extends Option<T>> extends StyledElement<T>
Represents the HTML <option> element for defining options in a select list.

The Option class provides a Java representation of the HTML option element, which defines individual selectable items within a <select> dropdown or <datalist> element. Each option has a value (submitted with the form) and display text (shown to the user).

Features:

  • Separate value and display text configuration
  • Selection state management
  • Optional label attribute for grouping
  • Disabled state support
  • Font inheritance from parent select
  • ID exclusion for lightweight rendering

Usage Example:


 // Create options with value and text
 Option option1 = new Option("us", "United States");
 Option option2 = new Option("ca", "Canada");
 Option option3 = new Option("mx", "Mexico");

 // Create a selected option
 Option defaultOption = new Option("us", "United States");
 defaultOption.setSelected(true);

 // Create an option with label
 Option labeledOption = new Option("ny", "New York");
 labeledOption.setLabel("Northeast Region");

 // Add options to a select
 Select countrySelect = new Select("country");
 countrySelect.addOption(option1);
 countrySelect.addOption(option2);
 countrySelect.addOption(option3);

 // Get option details
 String value = option1.getValue();    // "us"
 String text = option1.getText();      // "United States"
 boolean selected = option1.isSelected(); // true/false
 
Since:
2007
Version:
1.0
Author:
Marvin P. Warble Jr.
See Also:
  • Constructor Details

    • Option

      public Option()
      Creates a new Option element with no value or text.
    • Option

      public Option(String value)
      Creates a new Option with the specified value. The display text is set to the same value.
      Parameters:
      value - The value for this option, also used as the display text.
    • Option

      public Option(String value, String text)
      Creates a new Option with the specified value and display text.
      Parameters:
      value - The value submitted when this option is selected.
      text - The display text shown to the user.
  • Method Details

    • setDisabled

      public final T setDisabled(boolean disabled)
      Sets the disabled boolean attribute.

      Indicates that the element should be disabled, preventing user interaction and form submission. Used on <button>, <fieldset>, <input>, <select>, and <textarea> elements. Disabled elements are typically rendered in a grayed-out style.

      Overrides:
      setDisabled in class VisualElement<T extends Option<T>>
      Parameters:
      disabled - True to disable, false to enable.
      Returns:
      This element for method chaining.
    • setLabel

      public final T setLabel(String label)
      Sets the label text for this option.

      If set, the label text is displayed instead of the option's text content.

      Parameters:
      label - The label text for this option.
      Returns:
      This element for method chaining.
    • setSelected

      public final T setSelected(boolean selected)
      Sets whether this option is selected.
      Parameters:
      selected - True if this option should be selected.
      Returns:
      This element for method chaining.
    • setValue

      public final T setValue(String value)
      Sets the value attribute for this option, which is submitted with the form.
      Parameters:
      value - The value for this option.
    • setText

      public final void setText(String text)
      Sets the display text shown to the user for this option.
      Overrides:
      setText in class Element<T extends Option<T>>
      Parameters:
      text - The display text.
    • getText

      public final String getText()
      Returns the display text for this option.
      Returns:
      The display text.
    • isSelected

      public final boolean isSelected()
      Returns whether this option is currently selected.
      Returns:
      true if this option is selected, false otherwise.
    • getValue

      public final String getValue()
      Returns the value attribute for this option.
      Returns:
      The option value.