Class Toast


  • public class Toast
    extends java.lang.Object
    A Toast is a short-lived informational message displayed in the UI.

    Generally, Toasts are displayed in the bottom right corner of the UI - either the screen or the window. They are meant for non-critical information display not required by the current user workflow. For example if a licenses was about to expire, a Toast might be used to display the time left until expiration.

    This Toast class represents a reusable container that can display text in a user-defined position on the screen. Although the only limits to the position are that they must be fully on-screen, it will be generally useful to ensure it is positions towards the bottom of the display as the Toast will animate on screen from below. This Toast will have an in-animation that moves the message into view from off the bottom of the screen and an out- animation that fades the screen out. Animations are handled in a thread to avoid blocking the UI or scripting thread context

    Use the showToast(String, Rectangle) method to display the message in the location of interest:

             Toast toast = new Toast();
    
             String message = "Title\nThis is a brief message about something, but no worries, won't kill the application.";
             Rectangle position = new Rectangle(1400, 1000, 500, 80);
             toast.showToast(message, position);
     

    TODO: I have found the threading can cause slower than expected motion when the computer is under heavy load.

    • Constructor Summary

      Constructors 
      Constructor Description
      Toast()  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.lang.String getLastMessage()  
      int getToastTimeInSeconds()  
      void setToastTimeInSeconds​(int seconds)  
      void showLastToast()
      Reshow the last message that was displayed, in the same location it was displayed in.
      void showToast​(java.lang.String message, java.awt.Rectangle targetPosition)
      Display the provided message at the position specified.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • Toast

        public Toast()
    • Method Detail

      • setToastTimeInSeconds

        public void setToastTimeInSeconds​(int seconds)
        Parameters:
        seconds - The period of time, in seconds, the Toast should remain on screen after the in-animation completes and before the out-animation starts. The full time on screen will be longer than this as the animation runs. Defaults to 5 seconds if not set.
      • getToastTimeInSeconds

        public int getToastTimeInSeconds()
        Returns:
        The period of time, in seconds, the Toast will be displayed on screen after it reaches its destination and before it begins to fade out. The actual time on screen will be longer than this as the animations run.
      • getLastMessage

        public java.lang.String getLastMessage()
        Returns:
        The string contents of the last message sent to the Toast
      • showToast

        public void showToast​(java.lang.String message,
                              java.awt.Rectangle targetPosition)
        Display the provided message at the position specified.

        When this method is called, the Toast will be created and animate in by sliding up to the desired position from off the bottom of the screen. Once in position it holds for several seconds then fades away. This tool does not manage the display of multiple toasts at one time.

        Parameters:
        message - A message to display. The message can be multi-line, can include line breaks (\n) and will word wrap.
        targetPosition - A Rectangle defining the location and size of the box to display the message in. The location is relative to the top left of the screen (not window). The width will be fixed at the defined size, and the height will be maximized at the defined size (but could be smaller if the message wouldn't fill the area).
        Throws:
        java.lang.RuntimeException - if the target position is illegal, such as fully or partially off-screen.
      • showLastToast

        public void showLastToast()
        Reshow the last message that was displayed, in the same location it was displayed in.