1

Basically, I'm just trying to automatically open one stage every second as soon as the program starts. I was going to continue down this route while having multiple Timelines that uses different stages so I can put different images in it. What's the more practical way of doing this?

    mediaPlayer.play();
    int seconds = 1;
    Timeline timeline = new Timeline(
            new KeyFrame(Duration.seconds(seconds), e -> {
                BorderPane bpnew = new BorderPane();
                Scene repscene = new Scene(bpnew, 400, 450);
                Stage repstage = new Stage();
                repstage.setScene(repscene);
                repstage.show();
            })
        );
    timeline.setCycleCount(Animation.INDEFINITE);
    timeline.play();

    BorderPane root = new BorderPane();

    Scene scene = new Scene(root, 400, 450);
    primaryStage.setScene(scene);
    primaryStage.setTitle("Popup Test");
    primaryStage.show();
}
Mnai
  • 407
  • 3
  • 8
  • does it have to be stage? also why do you need it? what is the purpose? you can do this using JavaFX Alert or maybe write your own internal frame like in this answer https://stackoverflow.com/questions/17673292/internal-frames-in-javafx – Shawn Mar 02 '19 at 02:20
  • Each window should be independent so internal windows would not work. Alert windows look interesting but how would I implement a timer loop. I'm using a stage soi can put an image on it. The whole point is to have windows popup with different images. – Mnai Mar 02 '19 at 02:33

1 Answers1

2

how about something like this code. you can create a custom Dialog. then just place it in your timer. you can also use AnimationTimer. works like TimeLine but it gets called 60 times a second with no other restriction.

    Timeline timeline = new Timeline(
            new KeyFrame(Duration.seconds(5), e -> {
                Dialog< Void> dialog = new Dialog<>();
                dialog.setTitle( "Image");
                dialog.getDialogPane().getButtonTypes().add(ButtonType.OK);

                ImageView view = new ImageView("https://upload.wikimedia.org/wikipedia/commons/1/11/Cheetah_Kruger.jpg");

                ScrollPane root = new ScrollPane( view);
                root.setPrefWidth( 800);
                root.setPrefHeight( 600);

                dialog.getDialogPane().setContent( root);
                dialog.setResizable( true);
                dialog.show();
            })
        );
    timeline.setCycleCount(Animation.INDEFINITE);
    timeline.play();

few more options for periodic timers.

Shawn
  • 403
  • 8
  • 38
  • So basically using timeline is an appropriate method? after reading the documentation I know timeline() is used for animation which is why I am asking if there is a more recommended method that can be used as a trigger. But from your answer, i am assuming Timeline is indeed the most common method or just viable in this case? – Mnai Mar 02 '19 at 03:50
  • 1
    i am not gonna say the most common, it realy depends on what you are doing. like i said you can also use AnimationTimer which has less functionality and depends on you to complete it. now in your case i think TimeLine works but since i don't realy understand your logic such as why you want to open so many windows indefinitely, it is hard to say if this is the best or not. normally assuming this is some sort of an image viewer i would show the next one by some sort of button action instead of timer. – Shawn Mar 02 '19 at 03:56
  • 1
    here you can find a few more periodic timer solutions https://stackoverflow.com/questions/9966136/javafx-periodic-background-task – Shawn Mar 02 '19 at 03:59
  • The purpose is to recreate a silly program I made when I was younger but with java. Primarily to be annoying like how ads popup when you're on the internet. End result is to just open a bunch of windows in a random X, Y location with different pictures in each window. Purely self-entertainment. – Mnai Mar 02 '19 at 04:12
  • 1
    lol, then you are overthinking it. choose any of the methods and have fun. my recommendation is to make your app with each of the methods and choose the one you like. – Shawn Mar 02 '19 at 04:14
  • 1
    The best advice I have ever received. I know you're not supposed to say thank you here but I am doing it anyways, thank you. – Mnai Mar 02 '19 at 04:20