Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android toast equivalent in JavaFX

Tags:

javafx

I'm new to JavaFX and I wonder if there is some equivalent to Android Toast? I have seen the class Notification, but it doesn't look like it can be displayed only in the application. I also found I could user a Timer and make shading of a Label, but if there's some class to use, I'd be better!

Thanks!

like image 307
Synor Avatar asked Nov 07 '14 01:11

Synor


People also ask

What can I use instead of toast Android?

Alternatives to using toasts If your app is in the foreground, consider using a snackbar instead of using a toast. Snackbars include user-actionable options, which can provide a better app experience. If your app is in the background, and you want users to take some action, use a notification instead.

What is the difference between the toast class and the NotificationManager class?

The toast class is used to display alerts to the user; it disappears after a few seconds. The NotificationManager class is used to display notifications on the device's status bar.

Why is toast not working in Android Studio?

This is what I learnt as things to remember when showing a toast while debugging this issue : Make sure not to forget to call show() after the makeText. Check for the Context , if its the right one. The most important one , make sure your Android Notifications are on for your app, else the Toast will not be shown.


2 Answers

I know it has been a long time since you post this, but I have just made an android-like toast message for javafx, so I post it here in case that someone need this kind of code.

import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import javafx.util.Duration;

public final class Toast
{
    public static void makeText(Stage ownerStage, String toastMsg, int toastDelay, int fadeInDelay, int fadeOutDelay)
    {
        Stage toastStage=new Stage();
        toastStage.initOwner(ownerStage);
        toastStage.setResizable(false);
        toastStage.initStyle(StageStyle.TRANSPARENT);

        Text text = new Text(toastMsg);
        text.setFont(Font.font("Verdana", 40));
        text.setFill(Color.RED);

        StackPane root = new StackPane(text);
        root.setStyle("-fx-background-radius: 20; -fx-background-color: rgba(0, 0, 0, 0.2); -fx-padding: 50px;");
        root.setOpacity(0);

        Scene scene = new Scene(root);
        scene.setFill(Color.TRANSPARENT);
        toastStage.setScene(scene);
        toastStage.show();

        Timeline fadeInTimeline = new Timeline();
        KeyFrame fadeInKey1 = new KeyFrame(Duration.millis(fadeInDelay), new KeyValue (toastStage.getScene().getRoot().opacityProperty(), 1)); 
        fadeInTimeline.getKeyFrames().add(fadeInKey1);   
        fadeInTimeline.setOnFinished((ae) -> 
        {
            new Thread(() -> {
                try
                {
                    Thread.sleep(toastDelay);
                }
                catch (InterruptedException e)
                {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                   Timeline fadeOutTimeline = new Timeline();
                    KeyFrame fadeOutKey1 = new KeyFrame(Duration.millis(fadeOutDelay), new KeyValue (toastStage.getScene().getRoot().opacityProperty(), 0)); 
                    fadeOutTimeline.getKeyFrames().add(fadeOutKey1);   
                    fadeOutTimeline.setOnFinished((aeb) -> toastStage.close()); 
                    fadeOutTimeline.play();
            }).start();
        }); 
        fadeInTimeline.play();
    }
}

You can make a toast message from any class with this code:

String toastMsg = "some text...";
int toastMsgTime = 3500; //3.5 seconds
int fadeInTime = 500; //0.5 seconds
int fadeOutTime= 500; //0.5 seconds
Toast.makeText(primarystage, toastMsg, toastMsgTime, fadeInTime, fadeOutTime);
like image 74
alcoolis Avatar answered Sep 23 '22 08:09

alcoolis


Try the third party ControlsFX Notifications or Notification Pane.

"The NotificationPane control allows you to notify your users of something without requiring their immediate input (which you can do with the ControlsFX dialogs API). The NotificationPane will animate in and out of view"

toast1

Notifications "will show a notification message to users in one of nine locations on the screen ... After a set duration, the notification will fade out."

toast2

like image 31
jewelsea Avatar answered Sep 24 '22 08:09

jewelsea