3

I've been using JavaFx lately, as a beginner, and have been very impressed. At the moment I'm stuck trying to set a pagination slideshow to automatically move the slideshow forward every 5 seconds (and back to the first slide to continue when the last slide is reached). Can any one steer me in the right direction here?

    @FXML
public void slideshow(ActionEvent event) {
    // TODO Auto-generated method stub
    String[] photos = { "housestark.jpg", "housefrey.jpg", "housebar.jpg",
            "HouseBolton.jpg", "housegreyjoy.jpg", "houseaaryn.jpg",
            "houselannis.jpg", "housemart.jpg", "housereed.jpg",
            "housetully.jpg", "housetyrel.jpg", };
    Pagination p = new Pagination(photos.length);
    p.setPageFactory((Integer pageIndex) -> {
        return new ImageView(getClass().getResource(photos[pageIndex])
                .toExternalForm());
    });

    Stage stage = new Stage();
    stage.setScene(new Scene(p));
    stage.setX(1250);
    stage.setY(10);
    stage.setTitle("Slideshow");
    stage.setResizable(false);
    stage.show();
}

This is my code so far! I would appreciate any help anyone could give?

1 Answers1

3

It's pretty easy. All you have to do is create a timer that runs every 5 seconds, and when it runs move the page index.

public class SO extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage stage) {
        Pagination p = new Pagination(10);

        Timeline fiveSecondsWonder = new Timeline(new KeyFrame(Duration.seconds(5), event -> {
            int pos = (p.getCurrentPageIndex()+1) % p.getPageCount();
            p.setCurrentPageIndex(pos);
        }));
        fiveSecondsWonder.setCycleCount(Timeline.INDEFINITE);
        fiveSecondsWonder.play();

        stage.setScene(new Scene(p));
        stage.show();
    }
}

the five second wonder came from here: JavaFX periodic background task

Community
  • 1
  • 1
J Atkin
  • 3,080
  • 2
  • 19
  • 33