Class Snackbar


  • public class Snackbar
    extends Message
    Snackbars contain a single line of text directly related to the operation performed. They may contain a text action, but no icons. They provides brief feedback about an operation through a message at the bottom of the screen.

    Snackbars can also offer the ability to perform an action, such as undoing an action that was just taken, or retrying an action that had failed. The action needs to have a name and should be specified by passing a String to setActionText(String). The action can be set via setOnAction(EventHandler).

    Default duration for Snackbars is Message.LENGTH_LONG.

    Example

    The following code creates a Button and a Snackbar and in the event handler of the button calls show() to show the Snackbar.

     
     Button button = new Button("Click");
     Snackbar snackbar = new Snackbar("This is a Message!", "Action", e -> System.out.println("Action Pressed"));
     button.setOnAction(event -> snackbar.show());
     
     

    Screenshot of Snackbar control

    Since:
    5.0.0
    • Constructor Detail

      • Snackbar

        public Snackbar​(String message)
        Creates a new instance of Snackbar with the provided message.
        Parameters:
        message - Message to be show on the snackbar.
      • Snackbar

        public Snackbar​(String message,
                        String actionText,
                        EventHandler<ActionEvent> eventHandler)
        Creates a new instance of Snackbar with the provided message and a button.
        Parameters:
        message - Message to be show on the snackbar.
        actionText - Text for the action..
        eventHandler - Action to be performed by the button.
    • Method Detail

      • getActionText

        public final String getActionText()
        Gets the value of the property actionText.
        Property description:
        Text to be displayed beside the message on the Snackbar. When the text is clicked, the action provided via setOnAction(EventHandler) is fired.
      • setActionText

        public final void setActionText​(String value)
        Sets the value of the property actionText.
        Property description:
        Text to be displayed beside the message on the Snackbar. When the text is clicked, the action provided via setOnAction(EventHandler) is fired.
      • getOnAction

        public final EventHandler<ActionEvent> getOnAction()
        Gets the value of the property onAction.
        Property description:
        Action to be performed by the action text of the Snackbar. Note: The button won't be visible if no button text is provided, or if empty string is provided.
      • setOnAction

        public final void setOnAction​(EventHandler<ActionEvent> value)
        Sets the value of the property onAction.
        Property description:
        Action to be performed by the action text of the Snackbar. Note: The button won't be visible if no button text is provided, or if empty string is provided.
      • show

        public void show()
        Shows the Message immediately, if no other message is showing. If a message is already showing, it gets added to the waiting queue and will show when its turn arrives.
        Specified by:
        show in class Message
      • cancel

        public void cancel()
        If the Message is currently showing, calling this method will hide it immediately and show the next Message in queue. If the Message is waiting in the queue, calling this method removes the Message from the queue and the Message is never shown.
        Specified by:
        cancel in class Message