Class OptGroup<T extends OptGroup<T>>


public class OptGroup<T extends OptGroup<T>> extends StyledElement<T>
Represents an HTML <optgroup> element that groups related options in a select list.

The <optgroup> element is used within a <select> element to group related <option> elements together. This improves usability and accessibility by organizing options into logical categories with visible labels.

Features:

  • Groups related options in dropdown lists
  • Displays a visible label for each group
  • Improves organization of long option lists
  • Enhances accessibility and usability
  • Can be disabled to prevent selection of contained options
  • Supports nested option elements

Usage:


 Select carSelect = new Select("vehicle");
 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

    • OptGroup

      public OptGroup()
      Constructs a new OptGroup element.

      Creates an HTML <optgroup> element ready to contain grouped options. A label should be set to provide a visible heading for the group.

  • 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 OptGroup<T>>
      Parameters:
      disabled - True to disable, false to enable.
      Returns:
      This element for method chaining.
    • setLabel

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

      The label is displayed as the group heading in the dropdown list.

      Parameters:
      text - The group label text.
      Returns:
      This element for method chaining.
    • addOption

      public void addOption(Option option)
      Adds an option element to this group.

      Adds a fully configured <option> element to this optgroup. This allows for complete control over the option's attributes and content.

      Parameters:
      option - the Option element to add to this group
    • addOption

      public void addOption(String value)
      Adds a simple option to this group with the same value and display text.

      Convenience method that creates an option element where both the value attribute and the displayed text are the same. This is useful for simple dropdown lists where the displayed text and submitted value are identical.

      Parameters:
      value - the value and display text for the option
    • removeOption

      public void removeOption(Option option)
      Removes an option element from this group.
      Parameters:
      option - the Option element to remove
    • removeAllElements

      public void removeAllElements()
      Removes all option elements from this group.
      Overrides:
      removeAllElements in class Element<T extends OptGroup<T>>