I have the following code that runs at program startup. It serves to show a toast informing which people are birthday on the current day, through an excel file. However, when there is more than one birthday person, he shows several toasts at once, one on top of the other. I wanted a way to wait, while a toast disappears, to show the next one
Arquivo arquivo = new Arquivo();
ObservableList<String> listaDatas = arquivo.pegarListaString(1, 8); //take people's birthday dates
String data = Agenda.dateParaString(LocalDate.now()).substring(0, 5); //today's day
String data2 = Agenda.dateParaString(LocalDate.now().plusDays(1)).substring(0, 5); //tomorrow
int index = 0;
for(String valor: listaDatas) {
if (valor.length() > 4) {
if (valor.substring(0, 5).equalsIgnoreCase(data)) {
Agenda.mostrarToastTexto("Hoje é aniversário de " + arquivo.pegarValor(1, index, 0), 2000);
} else if(valor.substring(0, 5).equalsIgnoreCase(data2)) {
Agenda.mostrarToastTexto("Amanhã é aniversário de " + arquivo.pegarValor(1, index, 0) , 2000);
}
}
index++;
}
I already tried to use a "Thread.sleep" and also the class Timer, but to no avail.
Here is one approach using code from here.
This code uses Timeline
to show a different name every two seconds.
import com.jfoenix.controls.JFXSnackbar;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.util.Duration;
/**
*
* @author blj0011
*/
public class JavaFXApplication207 extends Application
{
@Override
public void start(Stage primaryStage)
{
StackPane root = new StackPane();
JFXSnackbar snackbar = new JFXSnackbar(root);
List<String> listaDatas = new ArrayList();
listaDatas.add("Kim");
listaDatas.add("John");
listaDatas.add("Chris");
AtomicInteger counter = new AtomicInteger();
Timeline threeSecondsWonder= new Timeline(new KeyFrame(Duration.seconds(3), new EventHandler<ActionEvent>()
{
@Override
public void handle(ActionEvent event)
{
snackbar.show("Happy birthday " + listaDatas.get(counter.getAndIncrement()) + "!", 2000);
}
}));
threeSecondsWonder.setCycleCount(listaDatas.size());
threeSecondsWonder.play();
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
launch(args);
}
}
Update: using JFXSnackbar
in the answer.