Class Label<T extends Label<T>>


public class Label<T extends Label<T>> extends StyledContainerElement<T>
Represents the HTML <label> element for labeling form controls.

The Label class provides a Java representation of the HTML label element, which defines a text label for form input elements. Labels improve form accessibility and usability by providing descriptive text that can be clicked to focus the associated input control.

Features:

  • Association with form controls via 'for' attribute
  • Form association for labels outside form boundaries
  • Click-to-focus functionality for associated inputs
  • Improved accessibility for screen readers
  • Text content support for descriptive labeling

Usage Example:


 // Create a label for a text input
 TextInput emailInput = new TextInput("email");
 emailInput.setId("emailInput");

 Label emailLabel = new Label("Email Address:");
 emailLabel.setFor("emailInput");

 // Create a label with form association
 Label formLabel = new Label("Username:");
 formLabel.setFor("usernameInput");
 formLabel.setForm("loginForm");

 // Create a simple label
 Label nameLabel = new Label();
 nameLabel.setText("Full Name:");
 nameLabel.setFor("nameField");
 
Since:
2007
Version:
1.0
Author:
Marvin P. Warble Jr.
See Also:
  • Constructor Details

    • Label

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

      public Label(String text)
      Creates a new Label element with the specified text.
      Parameters:
      text - The label text.
  • Method Details

    • setFor

      public final T setFor(String elementId)
      Sets the ID of the form control this label is associated with.

      Clicking this label transfers focus to the associated form control.

      Parameters:
      elementId - The ID of the associated form element.
      Returns:
      This element for method chaining.
    • setForm

      public final T setForm(String formId)
      Associates this label with a form element.
      Parameters:
      formId - the ID of the form element to associate with
      Returns:
      This element for method chaining.