JavaFX Scene Switch Animation

We all love JavaFX for its modern look and easy to use user interface APIs. In this article we will discuss about JavaFX Scene Switch (Change) animation. I have implemented two types of scene change animation. Fading Scenes and Sliding Scenes.

By default, JavaFX animation api does not provide a direct option animate a scene change. But we can use the following way.

  1. Load primary stage with a scene. Enclose this scene in a root container. This container can be of your preference. StackPane, Group etc can be used.
  2. When changing scenes, remove everything from this root container.
  3. Load the scene to be loaded to a variable. Add it new scene to the root container with animation.

Sliding Scene Transition

In this animation, we will get a nice sliding effect for the scenes. You can see the preview in the following GIF.

JavaFX Scene Sliding Animation
JavaFX Scene Sliding Animation

The idea here is to load the new scene beyond the visibility of the screen. For example, if the window height is 500px, then load the new scene at 500px. Then using KeyFrame animation, take the scene translateY to 0. This creates a slide in effect from bottom. Using translateX, we can implement slide in effect from sides.

Let’s see this in code. Following file is the first scene’s controller. When loadSecondScene() is called, second scene will be added.

/**
 * Removed Imports for code simplicity
 * FILE - Scene 1 Controller. On button Click, Scene 2 will be loaded  
 * @author Genuine Coder
 */
public class FirstSceneController implements Initializable {
    @FXML
    private AnchorPane anchorRoot;
    @FXML
    private StackPane parentContainer;

    @FXML
    private void loadSecondScene(ActionEvent event) throws IOException {
        Parent root = FXMLLoader.load(getClass().getResource("myscene2.fxml"));
        Scene scene = anchorRoot.getScene();
        //Set Y of second scene to Height of window
        root.translateYProperty().set(scene.getHeight());
        //Add second scene. Now both first and second scene is present
        parentContainer.getChildren().add(root);

        //Create new TimeLine animation
        Timeline timeline = new Timeline();
        //Animate Y property
        KeyValue kv = new KeyValue(root.translateYProperty(), 0, Interpolator.EASE_IN);
        KeyFrame kf = new KeyFrame(Duration.seconds(1), kv);
        timeline.getKeyFrames().add(kf);
        //After completing animation, remove first scene
        timeline.setOnFinished(t -> {
            parentContainer.getChildren().remove(anchorRoot);
        });
        timeline.play();
    }
}

Watch the Slide Transition in action from Genuine Coder Channel

Fading Scene Transition

Fading screen transition can also be implemented with a similar concept as of Sliding Screen transition.  The idea is, we apply a fade out transition for the first scene. After completion of fade out transition of first scene, we add second scene. The second scene will be added with zero opacity. Then using fade in transition we make the second scene visible.

You can see the program in action in the following Genuine Coder Tutorial video.

Comments

52 responses to “JavaFX Scene Switch Animation”

  1. … [Trackback]

    […] Read More here to that Topic: genuinecoder.com/javafx-scene-switch-change-animation/ […]

  2. … [Trackback]

    […] Find More Information here to that Topic: genuinecoder.com/javafx-scene-switch-change-animation/ […]

  3. … [Trackback]

    […] Read More on that Topic: genuinecoder.com/javafx-scene-switch-change-animation/ […]

  4. … [Trackback]

    […] Information on that Topic: genuinecoder.com/javafx-scene-switch-change-animation/ […]

  5. … [Trackback]

    […] Find More on to that Topic: genuinecoder.com/javafx-scene-switch-change-animation/ […]

  6. … [Trackback]

    […] Here you can find 31220 more Information on that Topic: genuinecoder.com/javafx-scene-switch-change-animation/ […]

  7. … [Trackback]

    […] Read More on on that Topic: genuinecoder.com/javafx-scene-switch-change-animation/ […]

  8. … [Trackback]

    […] Read More here to that Topic: genuinecoder.com/javafx-scene-switch-change-animation/ […]

  9. … [Trackback]

    […] Read More on that Topic: genuinecoder.com/javafx-scene-switch-change-animation/ […]

  10. … [Trackback]

    […] Read More to that Topic: genuinecoder.com/javafx-scene-switch-change-animation/ […]

  11. … [Trackback]

    […] Read More here to that Topic: genuinecoder.com/javafx-scene-switch-change-animation/ […]

  12. … [Trackback]

    […] Find More Information here on that Topic: genuinecoder.com/javafx-scene-switch-change-animation/ […]

  13. … [Trackback]

    […] Read More Info here on that Topic: genuinecoder.com/javafx-scene-switch-change-animation/ […]

  14. … [Trackback]

    […] Read More Information here to that Topic: genuinecoder.com/javafx-scene-switch-change-animation/ […]

  15. … [Trackback]

    […] Read More to that Topic: genuinecoder.com/javafx-scene-switch-change-animation/ […]

  16. … [Trackback]

    […] Find More on that Topic: genuinecoder.com/javafx-scene-switch-change-animation/ […]

  17. … [Trackback]

    […] Read More Info here to that Topic: genuinecoder.com/javafx-scene-switch-change-animation/ […]

  18. … [Trackback]

    […] Read More Info here to that Topic: genuinecoder.com/javafx-scene-switch-change-animation/ […]

  19. … [Trackback]

    […] Read More on that Topic: genuinecoder.com/javafx-scene-switch-change-animation/ […]

  20. … [Trackback]

    […] Read More Info here on that Topic: genuinecoder.com/javafx-scene-switch-change-animation/ […]

  21. … [Trackback]

    […] Read More on that Topic: genuinecoder.com/javafx-scene-switch-change-animation/ […]

  22. … [Trackback]

    […] Read More on to that Topic: genuinecoder.com/javafx-scene-switch-change-animation/ […]

  23. … [Trackback]

    […] Info to that Topic: genuinecoder.com/javafx-scene-switch-change-animation/ […]

  24. … [Trackback]

    […] Find More Information here on that Topic: genuinecoder.com/javafx-scene-switch-change-animation/ […]

  25. … [Trackback]

    […] Read More here on that Topic: genuinecoder.com/javafx-scene-switch-change-animation/ […]

  26. … [Trackback]

    […] Read More Info here to that Topic: genuinecoder.com/javafx-scene-switch-change-animation/ […]

  27. … [Trackback]

    […] Info to that Topic: genuinecoder.com/javafx-scene-switch-change-animation/ […]

  28. … [Trackback]

    […] Information to that Topic: genuinecoder.com/javafx-scene-switch-change-animation/ […]

  29. … [Trackback]

    […] Here you can find 58555 additional Info on that Topic: genuinecoder.com/javafx-scene-switch-change-animation/ […]

  30. … [Trackback]

    […] Find More here to that Topic: genuinecoder.com/javafx-scene-switch-change-animation/ […]

  31. … [Trackback]

    […] Read More Information here on that Topic: genuinecoder.com/javafx-scene-switch-change-animation/ […]

  32. … [Trackback]

    […] There you can find 87694 additional Information on that Topic: genuinecoder.com/javafx-scene-switch-change-animation/ […]

  33. … [Trackback]

    […] Info to that Topic: genuinecoder.com/javafx-scene-switch-change-animation/ […]

  34. … [Trackback]

    […] Information on that Topic: genuinecoder.com/javafx-scene-switch-change-animation/ […]

  35. … [Trackback]

    […] Find More here on that Topic: genuinecoder.com/javafx-scene-switch-change-animation/ […]

  36. … [Trackback]

    […] Find More Info here to that Topic: genuinecoder.com/javafx-scene-switch-change-animation/ […]

  37. … [Trackback]

    […] There you can find 84646 additional Information on that Topic: genuinecoder.com/javafx-scene-switch-change-animation/ […]

  38. … [Trackback]

    […] Find More on on that Topic: genuinecoder.com/javafx-scene-switch-change-animation/ […]

  39. … [Trackback]

    […] Info on that Topic: genuinecoder.com/javafx-scene-switch-change-animation/ […]

  40. purchase androxal generic androxals

    online order androxal australia over the counter

  41. enclomiphene generic 100 mg at promotional price or free offer

    buying enclomiphene price discount

  42. get rifaximin cheap canada pharmacy

    get rifaximin cheap now

  43. online order xifaxan cheap prescription

    get xifaxan toronto canada

  44. staxyn prescription from doctors online

    how to order staxyn no prescription usa

  45. discount avodart generic for sale

    ordering avodart uk pharmacy

  46. kamagra canada sans ordonnance

    generique kamagra en spain

  47. get dutasteride canada with no prescription

    dutasteride toronto canada

  48. discount flexeril cyclobenzaprine cheap discount

    cheap flexeril cyclobenzaprine usa generic

  49. buy cheap gabapentin generic pharmacy usa

    purchase gabapentin uk buy over counter

  50. discount fildena cheap mastercard

    discount fildena generic extended release

  51. purchase itraconazole cheap genuine

    ordering itraconazole uk delivery

  52. koupit obecný kamagra

    kamagra na lince bez lテゥkaナ冱kテゥho pナ册dpisu

Leave a Reply