Class Notification

java.lang.Object
com.vaadin.flow.component.Component
com.vaadin.flow.component.notification.Notification
All Implemented Interfaces:
AttachNotifier, DetachNotifier, HasComponents, HasElement, HasEnabled, HasStyle, HasTheme, HasThemeVariant<NotificationVariant>, Serializable

@Tag("vaadin-notification") @NpmPackage(value="@vaadin/polymer-legacy-adapter",version="24.1.7") @NpmPackage(value="@vaadin/notification",version="24.1.7") @JsModule("@vaadin/polymer-legacy-adapter/style-modules.js") @JsModule("@vaadin/notification/src/vaadin-notification.js") @JsModule("./flow-component-renderer.js") public class Notification extends Component implements HasComponents, HasStyle, HasThemeVariant<NotificationVariant>
Notifications are used to provide feedback to the user. They communicate information about activities, processes, and events in the application.
Author:
Vaadin Ltd
See Also:
  • Constructor Details

    • Notification

      public Notification()
      Default constructor. Create an empty notification with component support and non-auto-closing

      Note: To mix text and child components in notification that also supports child components, use the Text component for the textual parts.

    • Notification

      public Notification(String text)
      Creates a Notification with the given String rendered as its HTML text, that does not close automatically.
      Parameters:
      text - the text of the Notification
    • Notification

      public Notification(String text, int duration)
      Creates a Notification with given String rendered as its HTML text and given Integer rendered as its duration.

      Set to 0 or a negative number to disable the notification auto-closing.

      Parameters:
      text - the text of the Notification
      duration - the duration in milliseconds to show the notification
    • Notification

      public Notification(String text, int duration, Notification.Position position)
      Creates a Notification with given text String, duration and position

      Set to 0 or a negative number to disable the notification auto-closing.

      Parameters:
      text - the text of the notification
      duration - the duration in milliseconds to show the notification
      position - the position of the notification. Valid enumerate values are TOP_STRETCH, TOP_START, TOP_CENTER, TOP_END, MIDDLE, BOTTOM_START, BOTTOM_CENTER, BOTTOM_END, BOTTOM_STRETCH
    • Notification

      public Notification(Component... components)
      Creates a notification with given components inside.

      Note: To mix text and child components in a component that also supports child components, use the Text component for the textual parts.

      Parameters:
      components - the components inside the notification
      See Also:
  • Method Details

    • show

      public static Notification show(String text, int duration, Notification.Position position)
      Shows a notification in the current page with given text, duration and position.
      Parameters:
      text - the text of the Notification
      duration - the duration in milliseconds to show the notification
      position - the position of the notification. Valid enumerate values are TOP_STRETCH, TOP_START, TOP_CENTER, TOP_END, MIDDLE, BOTTOM_START, BOTTOM_CENTER, BOTTOM_END, BOTTOM_STRETCH
      Returns:
      the notification
    • show

      public static Notification show(String text)
      Shows a notification in the current page with given text.

      This is the convenience method for show(String, int, Position) which uses default web-component values for duration (which is 5000 ms) and position (Position.BOTTOM_START).

      Parameters:
      text - the text of the Notification
      Returns:
      the notification
    • setText

      public void setText(String text)
      Set the text of the notification with given String

      NOTE: When mixing this method with Notification() and Notification(Component...). Method will remove all the components from the notification.

      Parameters:
      text - the text of the Notification
    • setPosition

      public void setPosition(Notification.Position position)
      Set position of the notification.

      Parameters:
      position - the position of the notification. Valid enumerate values are TOP_STRETCH, TOP_START, TOP_CENTER, TOP_END, MIDDLE, BOTTOM_START, BOTTOM_CENTER, BOTTOM_END, BOTTOM_STRETCH, not null
    • getPosition

      public Notification.Position getPosition()

      Description copied from corresponding location in WebComponent:

      Alignment of the notification in the viewport Valid values are top-stretch|top-start|top-center|top-end|middle|bottom-start|bottom-center|bottom-end|bottom-stretch

      This property is not synchronized automatically from the client side, so the returned value may not be the same as in client side.

      The default position value is Position.BOTTOM_START.

      Returns:
      the Notification.Position property from the webcomponent
    • open

      public void open()
      Opens the notification.
    • close

      public void close()
      Closes the notification.

      Note: This method also removes the notification component from the DOM after closing it, unless you have added the component manually.

    • add

      public void add(Collection<Component> components)
      Adds the given components into this notification.

      The elements in the DOM will not be children of the <vaadin-notification> element, but will be inserted into an overlay that is attached into the <body>.

      NOTE: When mixing this method with Notification(String), Notification(String, int) and Notification(String, int, Position) method will remove the text content.

      Specified by:
      add in interface HasComponents
      Parameters:
      components - the components to add
    • addComponentAtIndex

      public void addComponentAtIndex(int index, Component component)
      Adds the given component into this notification at the given index.

      The element in the DOM will not be child of the <vaadin-notification> element, but will be inserted into an overlay that is attached into the <body>.

      NOTE: When mixing this method with Notification(String), Notification(String, int) and Notification(String, int, Position) method will remove the text content.

      Specified by:
      addComponentAtIndex in interface HasComponents
      Parameters:
      index - the index, where the component will be added.
      component - the component to add
    • setOpened

      public void setOpened(boolean opened)
      Opens or closes the notification.

      Note: You don't need to add the component anywhere before opening it. Since <vaadin-notification>'s location in the DOM doesn't really matter, opening a notification will automatically add it to the <body> if it's not yet attached anywhere.

      Parameters:
      opened - true to open the notification, false to close it
    • isOpened

      @Synchronize(property="opened", value="opened-changed") public boolean isOpened()
      True if the notification is currently displayed.

      This property is synchronized automatically from client side when an opened-changed event happens.

      Returns:
      the opened property from the webcomponent
    • addOpenedChangeListener

      public Registration addOpenedChangeListener(ComponentEventListener<Notification.OpenedChangeEvent> listener)
      Adds a listener for opened-changed events fired by the webcomponent.
      Parameters:
      listener - the listener
      Returns:
      a Registration for removing the event listener
    • setDuration

      public void setDuration(int duration)
      The duration in milliseconds to show the notification. Set to 0 or a negative number to disable the notification auto-closing.
      Parameters:
      duration - the value to set
    • getDuration

      public int getDuration()

      The duration in milliseconds to show the notification. Set to 0 or a negative number to disable the notification auto-closing.

      This property is not synchronized automatically from the client side, so the returned value may not be the same as in client side.

      Returns:
      the duration property from the webcomponent
    • addAttachListener

      public Registration addAttachListener(ComponentEventListener<AttachEvent> listener)
      Adds a attach listener to this component.

      Note: To listen for opening the notification, you should use addOpenedChangeListener(ComponentEventListener).

      Specified by:
      addAttachListener in interface AttachNotifier
      Parameters:
      listener - the listener to add, not null
      Returns:
      a handle that can be used for removing the listener
    • addDetachListener

      public Registration addDetachListener(ComponentEventListener<DetachEvent> listener)
      Adds a detach listener to this component.

      Note: To listen for closing the notification, you should use addOpenedChangeListener(ComponentEventListener), as the component is not necessarily removed from the DOM when closing.

      Specified by:
      addDetachListener in interface DetachNotifier
      Parameters:
      listener - the listener to add, not null
      Returns:
      a handle that can be used for removing the listener
    • onAttach

      protected void onAttach(AttachEvent attachEvent)
      Description copied from class: Component
      Called when the component is attached to a UI.

      The default implementation does nothing.

      This method is invoked before the AttachEvent is fired for the component.

      Overrides:
      onAttach in class Component
      Parameters:
      attachEvent - the attach event
    • onDetach

      protected void onDetach(DetachEvent detachEvent)
      Description copied from class: Component
      Called when the component is detached from a UI.

      The default implementation does nothing.

      This method is invoked before the DetachEvent is fired for the component.

      Overrides:
      onDetach in class Component
      Parameters:
      detachEvent - the detach event
    • setClassName

      public void setClassName(String className)
      Sets the CSS class names of the notification overlay element. This method overwrites any previous set class names.
      Specified by:
      setClassName in interface HasStyle
      Parameters:
      className - a space-separated string of class names to set, or null to remove all class names
    • getClassNames

      public ClassList getClassNames()
      Description copied from interface: HasStyle
      Gets the set of CSS class names used for this element. The returned set can be modified to add or remove class names. The contents of the set is also reflected in the value of the class attribute.

      Despite the name implying a list being returned, the return type is actually a Set since the in-browser return value behaves like a Set in Java.

      Specified by:
      getClassNames in interface HasStyle
      Returns:
      a list of class names, never null
      See Also:
    • getStyle

      public Style getStyle()
      Description copied from interface: HasStyle
      Gets the style instance for managing inline styles for the element of this component.
      Specified by:
      getStyle in interface HasStyle
      Returns:
      the style object for the element, not null
      Throws:
      UnsupportedOperationException - Notification does not support adding styles to card element