Class Notification

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

@Tag("vaadin-notification") @NpmPackage(value="@vaadin/polymer-legacy-adapter",version="24.4.2") @NpmPackage(value="@vaadin/notification",version="24.4.2") @JsModule("@vaadin/polymer-legacy-adapter/style-modules.js") @JsModule("@vaadin/notification/src/vaadin-notification.js") @JsModule("./flow-component-renderer.js") public class Notification extends com.vaadin.flow.component.Component implements com.vaadin.flow.component.HasComponents, com.vaadin.flow.component.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:
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static class 
    opened-changed event is sent when the notification opened state changes.
    static enum 
    Enumeration of all available positions for notification component
  • Constructor Summary

    Constructors
    Constructor
    Description
    Default constructor.
    Notification(com.vaadin.flow.component.Component... components)
    Creates a notification with given components inside.
    Creates a Notification with the given String rendered as its HTML text, that does not close automatically.
    Notification(String text, int duration)
    Creates a Notification with given String rendered as its HTML text and given Integer rendered as its duration.
    Notification(String text, int duration, Notification.Position position)
    Creates a Notification with given text String, duration and position
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    add(Collection<com.vaadin.flow.component.Component> components)
    Adds the given components into this notification.
    com.vaadin.flow.shared.Registration
    addAttachListener(com.vaadin.flow.component.ComponentEventListener<com.vaadin.flow.component.AttachEvent> listener)
    void
    addComponentAtIndex(int index, com.vaadin.flow.component.Component component)
    Adds the given component into this notification at the given index.
    com.vaadin.flow.shared.Registration
    addDetachListener(com.vaadin.flow.component.ComponentEventListener<com.vaadin.flow.component.DetachEvent> listener)
    com.vaadin.flow.shared.Registration
    addOpenedChangeListener(com.vaadin.flow.component.ComponentEventListener<Notification.OpenedChangeEvent> listener)
    Adds a listener for opened-changed events fired by the webcomponent.
    void
    Closes the notification.
    com.vaadin.flow.dom.ClassList
     
    int
    The duration in milliseconds to show the notification.
    Description copied from corresponding location in WebComponent:
    com.vaadin.flow.dom.Style
     
    boolean
    True if the notification is currently displayed.
    protected void
    onAttach(com.vaadin.flow.component.AttachEvent attachEvent)
     
    protected void
    onDetach(com.vaadin.flow.component.DetachEvent detachEvent)
     
    void
    Opens the notification.
    void
    setClassName(String className)
    Sets the CSS class names of the notification overlay element.
    void
    setDuration(int duration)
    The duration in milliseconds to show the notification.
    void
    setOpened(boolean opened)
    Opens or closes the notification.
    void
    Set position of the notification.
    void
    Set the text of the notification with given String
    show(String text)
    Shows a notification in the current page with given text.
    show(String text, int duration, Notification.Position position)
    Shows a notification in the current page with given text, duration and position.

    Methods inherited from class com.vaadin.flow.component.Component

    addListener, findAncestor, fireEvent, from, get, getChildren, getElement, getEventBus, getId, getListeners, getLocale, getParent, getTranslation, getTranslation, getTranslation, getTranslation, getTranslation, getTranslation, getUI, hasListener, isAttached, isTemplateMapped, isVisible, onEnabledStateChanged, removeFromParent, scrollIntoView, scrollIntoView, set, setElement, setId, setVisible

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface com.vaadin.flow.component.HasComponents

    add, add, addComponentAsFirst, remove, remove, removeAll

    Methods inherited from interface com.vaadin.flow.component.HasElement

    getElement

    Methods inherited from interface com.vaadin.flow.component.HasEnabled

    isEnabled, setEnabled

    Methods inherited from interface com.vaadin.flow.component.HasStyle

    addClassName, addClassNames, getClassName, hasClassName, removeClassName, removeClassNames, setClassName

    Methods inherited from interface com.vaadin.flow.component.HasTheme

    addThemeName, addThemeNames, getThemeName, getThemeNames, hasThemeName, removeThemeName, removeThemeNames, setThemeName, setThemeName

    Methods inherited from interface com.vaadin.flow.component.shared.HasThemeVariant

    addThemeVariants, removeThemeVariants
  • 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(com.vaadin.flow.component.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:
      • HasComponents.add(Component...)
  • 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<com.vaadin.flow.component.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 com.vaadin.flow.component.HasComponents
      Parameters:
      components - the components to add
    • addComponentAtIndex

      public void addComponentAtIndex(int index, com.vaadin.flow.component.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 com.vaadin.flow.component.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 com.vaadin.flow.shared.Registration addOpenedChangeListener(com.vaadin.flow.component.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 com.vaadin.flow.shared.Registration addAttachListener(com.vaadin.flow.component.ComponentEventListener<com.vaadin.flow.component.AttachEvent> listener)

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

      Specified by:
      addAttachListener in interface com.vaadin.flow.component.AttachNotifier
    • addDetachListener

      public com.vaadin.flow.shared.Registration addDetachListener(com.vaadin.flow.component.ComponentEventListener<com.vaadin.flow.component.DetachEvent> listener)

      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 com.vaadin.flow.component.DetachNotifier
    • onAttach

      protected void onAttach(com.vaadin.flow.component.AttachEvent attachEvent)
      Overrides:
      onAttach in class com.vaadin.flow.component.Component
    • onDetach

      protected void onDetach(com.vaadin.flow.component.DetachEvent detachEvent)
      Overrides:
      onDetach in class com.vaadin.flow.component.Component
    • 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 com.vaadin.flow.component.HasStyle
      Parameters:
      className - a space-separated string of class names to set, or null to remove all class names
    • getClassNames

      public com.vaadin.flow.dom.ClassList getClassNames()
      Specified by:
      getClassNames in interface com.vaadin.flow.component.HasStyle
    • getStyle

      public com.vaadin.flow.dom.Style getStyle()
      Specified by:
      getStyle in interface com.vaadin.flow.component.HasStyle
      Throws:
      UnsupportedOperationException - Notification does not support adding styles to card element