Docs

Documentation versions (currently viewingVaadin 14)

You are viewing documentation for an older Vaadin version. View latest documentation

Enabled State

Components that allow user interaction, such as TextField or Button, can have three different enabled states:

  • Enabled: An enabled component allows the user to interact with it. This is the default state.

  • Explicitly disabled: A component is explicitly disabled when setEnabled(false) is called directly on it. The user cannot interact with the component, and communication from the client to the server is blocked.

  • Implicitly disabled: A component is implicitly disabled when it is a child of a explicitly disabled container. The component behaves exactly like an explicitly disabled component, except it is automatically enabled again as soon as it detaches from the disabled container.

Explicitly Enabling and Disabling Components

Any component that implements the HasEnabled interface can be explicitly enabled or disabled.

Example: Disabling a component using the component.setEnabled method.

TextField name = new TextField("Name");
name.setEnabled(false);
  • This disables the name field:

    • Users cannot interact with it, and

    • Events from the client to the server are blocked.

  • The server does not handle status updates from the component, even if the component is changed manually on the browser, for example by a client-side script or via a developer console.

Example: Disabling all components in a container by using the same API:

FormLayout form = new FormLayout();

TextField name = new TextField("Name");
TextField email = new TextField("E-mail");
Button submit = new Button("Submit");

form.add(name, email, submit);
// all children are implicitly disabled
form.setEnabled(false);
System.out.println(name.isEnabled()); // prints false

Implicitly Enabled and Disabled Components

When an implicitly disabled component is detached from a disabled container, it is automatically enabled again. Similarly, if an enabled component is attached to a disabled container, it is automatically implicitly disabled.

Examples: Implicitly enabled and disabled components

FormLayout form = new FormLayout();
form.setEnabled(false); // the entire form is disabled

TextField name = new TextField("Name");
// prints true, since it is not attached yet
System.out.println(name.isEnabled());

Button submit = new Button("Submit");
// the submit button is explicitly disabled
submit.setEnabled(false);
System.out.println(submit.isEnabled()); // prints false

form.add(name, submit); // attaches children

System.out.println(name.isEnabled()); // prints false
System.out.println(submit.isEnabled()); // prints false

form.remove(name); // the name field gets detached
System.out.println(name.isEnabled()); // prints true

form.remove(submit); // the submit button gets detached

// prints false, since it was explicitly disabled
System.out.println(submit.isEnabled());

Overriding Default Disabled Behavior

By default, disabled components do not allow user interaction from the client side. However, it is sometimes necessary for complex (composite) components to remain partially functional, even in the disabled state. For example, you may want to only fully enable a registration form after a user selects a checkbox to accept a license agreement.

Enabling Property Changes

You can override the default disabled behavior by enabling certain RPC-client-side calls that would normally be blocked for disabled components.

The first way to do this is to use the synchronizeProperty method with the DisabledUpdateMode.ALWAYS argument value.

Example: This Polymer template component controls its own enabled state via the checkbox. The checkbox is never disabled, and it enables and disables the component.

@Tag("registration-form")
@JsModule("./src/registration-form.js")
public class RegistrationForm
        extends PolymerTemplate<TemplateModel>
        implements HasEnabled {

    @Id
    private TextField name;

    @Id
    private TextField email;

    @Id
    private Button submit;

    @Id
    private Element enable;

    public RegistrationForm() {
        enable.synchronizeProperty("checked",
                "checked-changed",
                DisabledUpdateMode.ALWAYS);
        enable.addPropertyChangeListener("checked",
                this::handleEnabled);
        setEnabled(false);
    }

    private void handleEnabled(
            PropertyChangeEvent event) {
        setEnabled((Boolean) event.getValue());
    }

    @EventHandler
    private void register() {
        String userName = name.getValue();
        String userEmail = email.getValue();
        System.out.println("Register user with name='"
                + userName
                + "' and email='" + userEmail + "'");
    }
}

Here is its template file:

class RegistrationForm extends PolymerElement {

    static get template() {
        return html`
            <vaadin-text-field id='name'>
                {{name}}
            </vaadin-text-field>
            <vaadin-text-field id='email'>
                {{email}}
            </vaadin-text-field>
            <vaadin-button id='submit'
                on-click='register'>
                Register
            </vaadin-button>
            <vaadin-checkbox id='enable'>
                Accept License Agreement
            </vaadin-checkbox>`;
    }

    static get is() {
        return 'registration-form';
    }
}

customElements.define(RegistrationForm.is,
        RegistrationForm);
  • The checkbox is implicitly disabled if the template (which is its parent) is disabled. As a result, no RPC is allowed for the checkbox.

  • The synchronizeProperty method (with extra arguments) is used to synchronize the checked property.

    • The argument, DisabledUpdateMode.ALWAYS, is an enum value that allows updates for this property, even if the element is disabled.

  • The folowing RPC communications are blocked for the disabled element:

    • Property changes.

    • DOM events.

    • Event handler methods (annotated with @EventHandler). For example, the register() method is an event handler method that is blocked when the component is disabled.

    • Client delegate methods (annotated with @ClientCallable).

As an alternative, you can use the @Synchronize annotation with the DisabledUpdateMode.ALWAYS argument value.

Example: Using the @Synchronize annotation for the property getter in your component.

@Synchronize(property = "prop", value = "prop-changed",
             allowUpdates = DisabledUpdateMode.ALWAYS)
public String getProp() {
    return getElement().getProperty("prop");
}

Enabling DOM Events

There are two ways to enable DOM events. You can use:

  1. An addEventListener overload method in the Element API, or

  2. The @DomEvent annotation.

Example: Unblocking a DOM event for a disabled element using the addEventListener overload method that accepts the DisabledUpdateMode.ALWAYS parameter.

public Notification() {
    getElement().addEventListener("opened-changed",
            event -> System.out.println("Opened"))
      .setDisabledUpdateMode(DisabledUpdateMode.ALWAYS);
}

Example: Unblocking a DOM event for a disabled component using the @DomEvent annotation with the parameter value allowUpdates = DisabledUpdateMode.ALWAYS ):

@DomEvent(value = "click",
          allowUpdates = DisabledUpdateMode.ALWAYS)
public class CustomEvent
        extends ComponentEvent<Component> {
}

Enabling Server-Handler Methods

If there are server-handler methods annotated with @ClientCallable or @EventHandler, you can unblock them for disabled components by specifying DisabledUpdateMode.ALWAYS as a value.

Example: Specifying DisabledUpdateMode.ALWAYS

@EventHandler(DisabledUpdateMode.ALWAYS)
private void eventHandler() {
}

@ClientCallable(DisabledUpdateMode.ALWAYS)
private void clientRequest() {
}

08835FA6-AA75-42A8-80D8-C8DC22B62C1B