Class Button<T extends Button<T>>


public class Button<T extends Button<T>> extends StyledContainerElement<T>
Represents the HTML <button> element for creating clickable buttons.

The Button class provides a Java representation of the HTML button element, which creates a clickable button that can be used for form submission, triggering actions, or resetting forms. Unlike input buttons, button elements can contain rich content like images and formatted text.

Features:

  • Support for button types (submit, reset, button)
  • Form association and override capabilities
  • Custom form action, method, and encoding for submit buttons
  • Form validation control override
  • Target window specification override
  • Auto-focus capability for keyboard navigation
  • Named button values for form submission
  • Rich content support (text, images, icons)

Usage Example:


 // Create a submit button
 Button submitBtn = new Button("Submit Form");
 submitBtn.setType(ButtonType.SUBMIT);
 submitBtn.setName("submitButton");
 submitBtn.setValue("submitted");

 // Create a button with custom form action
 Button saveBtn = new Button("Save Draft");
 saveBtn.setType(ButtonType.SUBMIT);
 saveBtn.setFormAction("/save-draft");
 saveBtn.setFormMethod(FormMethod.POST);

 // Create a reset button
 Button resetBtn = new Button("Clear Form");
 resetBtn.setType(ButtonType.RESET);

 // Create a generic button for JavaScript actions
 Button actionBtn = new Button("Click Me");
 actionBtn.setType(ButtonType.BUTTON);
 actionBtn.setAutoFocus(true);
 
Since:
2007
Version:
1.0
Author:
Marvin P. Warble Jr.
See Also:
  • Constructor Details

    • Button

      public Button()
      Constructs an empty <button> element with no text or type specified.
    • Button

      public Button(String text)
      Constructs a <button> element with the specified text content.
      Parameters:
      text - The text to display on the button.
  • Method Details

    • setForm

      public final T setForm(String formId)
      Associates this element with a form element.

      Specifies the form this element belongs to using the form's ID. This is useful when the element is placed outside of its associated <form> in the document.

      Parameters:
      formId - the ID of the form element to associate with
      Returns:
      This element for method chaining.
    • setFormAction

      public final T setFormAction(String url)
      Overrides the form's action URL when this button is used to submit.

      Only valid for buttons with type="submit".

      If the URL starts with "/" (absolute path within the application), the servlet context path will be automatically prepended.

      Parameters:
      url - The URL to submit the form data to.
      Returns:
      this element for method chaining
    • setFormEncType

      public final T setFormEncType(FormEncoding encoding)
      Sets the form encoding type override using the FormEncoding enum.
      Parameters:
      encoding - The encoding type.
      Returns:
      This element for method chaining.
    • setFormEncType

      public final T setFormEncType(String encoding)
      Sets the formenctype attribute.

      Overrides the form's enctype attribute for this specific submit button. Used on <button> and <input> elements with type="submit" or type="image".

      Parameters:
      encoding - The encoding type for form submission.
      Returns:
      This element for method chaining.
    • setFormMethod

      public final T setFormMethod(FormMethod formMethod)
      Sets the form method override using the FormMethod enum.
      Parameters:
      formMethod - The HTTP method (GET or POST).
      Returns:
      This element for method chaining.
    • setFormMethod

      public final T setFormMethod(String formMethod)
      Sets the formmethod attribute.

      Overrides the form's method attribute for this specific submit button. Used on <button> and <input> elements with type="submit" or type="image".

      Parameters:
      formMethod - The HTTP method for form submission (get, post).
      Returns:
      This element for method chaining.
    • setFormNoValidate

      public final T setFormNoValidate(boolean flag)
      Sets the formnovalidate boolean attribute.

      Overrides the form's validation behavior for this specific submit button, bypassing constraint validation. Used on <button> and <input> elements with type="submit" or type="image".

      Parameters:
      flag - True to skip form validation, false to remove.
      Returns:
      This element for method chaining.
    • setFormTarget

      public final T setFormTarget(Target target)
      Sets the form target override using the Target enum.
      Parameters:
      target - The browsing context for form submission.
      Returns:
      This element for method chaining.
    • setFormTarget

      public final T setFormTarget(String target)
      Sets the formtarget attribute.

      Overrides the form's target attribute for this specific submit button, specifying where to display the response. Used on <button> and <input> elements with type="submit" or type="image". Values include "_self", "_blank", "_parent", and "_top".

      Parameters:
      target - The browsing context for form submission.
      Returns:
      This element for method chaining.
    • setName

      public final T setName(String name)
      Sets the name attribute.

      Specifies the name of the element, used as a key when submitting form data or for programmatic access.

      Parameters:
      name - The name of the element.
      Returns:
      This element for method chaining.
    • setType

      public final T setType(ButtonType type)
      Sets the type of this button.

      The type determines the button's behavior:

      Parameters:
      type - The button type.
      Returns:
      this element for method chaining
    • setType

      public final T setType(String type)
      Sets the type of this button.
      Parameters:
      type - The button type as a string (e.g., "submit", "reset", "button").
      Returns:
      this element for method chaining
    • 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 Button<T>>
      Parameters:
      disabled - True to disable, false to enable.
      Returns:
      This element for method chaining.
    • setPopoverTarget

      public final T setPopoverTarget(String popoverTarget)
      Sets the ID of the popover element that this button controls.
      Parameters:
      popoverTarget - The ID of the popover target element.
      Returns:
      This element for method chaining.
    • setPopoverTargetAction

      public final T setPopoverTargetAction(String popoverTargetAction)
      Sets the action to perform on the target popover element.

      Values include "toggle" (default), "show", and "hide".

      Parameters:
      popoverTargetAction - The popover action (toggle, show, hide).
      Returns:
      This element for method chaining.
    • setValue

      public final T setValue(String value)
      Sets the value submitted with the form data when this button is used as the submitter.
      Parameters:
      value - The value for this button.
      Returns:
      This element for method chaining.
    • getForm

      protected String getForm()
      Returns the form attribute value.

      The form attribute associates this element with a form element that is not its ancestor.

      Returns:
      The associated form ID, or null if not set.
    • getName

      protected String getName()
      Returns the name attribute value.

      The name attribute specifies the name used to identify the element in form submissions.

      Returns:
      The name, or null if not set.
    • getFormAction

      protected String getFormAction()
      Returns the formaction attribute value.

      The formaction attribute overrides the action attribute of the parent form for this specific submit button. Used on <button type="submit"> and <input type="submit"> elements.

      Returns:
      The form action URL, or null if not set.
    • getFormEncType

      protected String getFormEncType()
      Returns the formenctype attribute value.

      The formenctype attribute overrides the enctype attribute of the parent form for this specific submit button. Used on <button type="submit"> and <input type="submit"> elements.

      Returns:
      The form encoding type, or null if not set.
    • getFormMethod

      protected String getFormMethod()
      Returns the formmethod attribute value.

      The formmethod attribute overrides the method attribute of the parent form for this specific submit button. Used on <button type="submit"> and <input type="submit"> elements. Values are "get", "post", or "dialog".

      Returns:
      The form method, or null if not set.
    • isFormNoValidate

      protected boolean isFormNoValidate()
      Returns whether the formnovalidate attribute is set.

      The formnovalidate attribute overrides the novalidate attribute of the parent form, bypassing constraint validation for this specific submit button. Used on <button type="submit"> and <input type="submit"> elements.

      Returns:
      true if formnovalidate is present, false otherwise.
    • getFormTarget

      protected String getFormTarget()
      Returns the formtarget attribute value.

      The formtarget attribute overrides the target attribute of the parent form for this specific submit button. Used on <button type="submit"> and <input type="submit"> elements. Values include "_self", "_blank", "_parent", and "_top".

      Returns:
      The form target, or null if not set.