1

I am trying to implement a Transition effect on a node, below is SSCE,

public class GridPaneExperiments extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {
        primaryStage.setTitle("Experiment");

        Button button2 = new Button("Expand");
        Button button3 = new Button("Button 3");
        Button button4 = new Button("Button 4");

        GridPane gridPane = new GridPane();

        ToolBar bar = new ToolBar();
        bar.getItems().addAll(button3, button4);

        gridPane.add(button2, 0, 0, 1, 1);
        gridPane.add(bar, 1, 0, 1, 1);

        //Handle Mouse on Button
        button2.setOnMouseEntered((MouseEvent event) -> {
            TranslateTransition openNav = new TranslateTransition(new Duration(350), bar);
            openNav.setToX(0);
            if (bar.getTranslateX() != 0) {
                openNav.play();
            }
        });

        button2.setOnMouseExited((MouseEvent event) -> {
            TranslateTransition closeNav = new TranslateTransition(new Duration(350), bar);
            closeNav.setToX(-(((GridPane) gridPane).getWidth()));
            closeNav.play();
        });
        //Handle Mouse on ToolBar
        bar.setOnMouseExited((MouseEvent event) -> {
            TranslateTransition closeNav = new TranslateTransition(new Duration(350), bar);
            closeNav.setToX(-(((GridPane) gridPane).getWidth()));
            closeNav.play();
        });

        bar.setOnMouseEntered((MouseEvent event) -> {
            TranslateTransition openNav = new TranslateTransition(new Duration(350), bar);
            openNav.setToX(0);
            if (bar.getTranslateX() != 0) {
                openNav.play();
            }
        });

        Scene scene = new Scene(gridPane, 240, 100);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

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

What I am trying to achieve is:

  1. When mouse entered the Button "Expand" a Node will be opened to its right, after which if mouse entered the opened node it should not get close .

  2. When mouse entered the Button "Expand" a Node will be opened and mouse exited from Button "Expand" (but not entered the opened node) , then the opened node should be closed.

Currently I am playing the Transition animation in Mouse events of both Button and Node.

How can I achieve this?

Son Truong
  • 10,947
  • 5
  • 23
  • 50
user3164187
  • 1,278
  • 2
  • 17
  • 42
  • 2
    This sounds very similar to [`ControlsFx`](http://fxexperience.com/controlsfx/) [`PopOver`](https://controlsfx.bitbucket.io/org/controlsfx/control/PopOver.html). Quick example [here](https://stackoverflow.com/questions/21655790/javafx-popover-from-controlfx). The example I created uses `Label` instead of `Button`. – Sedrick Aug 30 '18 at 13:35

2 Answers2

4

Just use a single animation for closing and opening. This way you can reverse the animation easily, don't run the risk of starting multiple animations in parallel and starting a closing animation is not an issue, since you change the animation to a opening animation when entering one of the nodes:

@Override
public void start(Stage primaryStage) throws Exception {
    primaryStage.setTitle("Experiment");

    Button button2 = new Button("Expand");
    Button button3 = new Button("Button 3");
    Button button4 = new Button("Button 4");

    GridPane gridPane = new GridPane();

    ToolBar bar = new ToolBar();
    bar.getItems().addAll(button3, button4);
    bar.setTranslateX(-10000); // somewhere outside view

    gridPane.add(button2, 0, 0, 1, 1);
    gridPane.add(bar, 1, 0, 1, 1);

    TranslateTransition transition = new TranslateTransition(Duration.millis(300), bar);
    transition.setToX(0);

    EventHandler<MouseEvent> enterHandler = (MouseEvent event) -> {
        transition.setFromX(-gridPane.getWidth());
        Duration time = transition.getCurrentTime();
        transition.setRate(1);
        transition.playFrom(time);
    };

    EventHandler<MouseEvent> exitHandler = (MouseEvent event) -> {
        if (!(button2.isHover() || bar.isHover())) {
            Duration time = transition.getCurrentTime();
            transition.setRate(-1);
            transition.playFrom(time);
        }
    };

    //Handle Mouse on Button
    button2.setOnMouseEntered(enterHandler);
    bar.setOnMouseEntered(enterHandler);
    button2.setOnMouseExited(exitHandler);
    bar.setOnMouseExited(exitHandler);


    Scene scene = new Scene(gridPane, 240, 100);
    primaryStage.setScene(scene);
    primaryStage.show();
}
fabian
  • 67,623
  • 12
  • 74
  • 102
2

you can simply achive your task using a helper HBox and setting setOnMouseExited to only that:

public class GridPaneExperiments extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {
        primaryStage.setTitle("Experiment");

        Button button2 = new Button("Expand");
        Button button3 = new Button("Button 3");
        Button button4 = new Button("Button 4");

        ToolBar bar = new ToolBar();
        bar.getItems().addAll(button3, button4);

        GridPane gridPane = new GridPane();

        HBox hbox = new HBox(button2, bar);
        hbox.setStyle("-fx-border-color: red");

        gridPane.add(hbox, 0, 0);

        //Handle Mouse on Button
        button2.setOnMouseEntered((MouseEvent event) -> {
            TranslateTransition openNav = new TranslateTransition(new Duration(350), bar);
            openNav.setToX(0);
            if (bar.getTranslateX() != 0) {
                openNav.play();
            }
        });

//        button2.setOnMouseExited((MouseEvent event) -> {
//            TranslateTransition closeNav = new TranslateTransition(new Duration(350), bar);
//            closeNav.setToX(-(((GridPane) gridPane).getWidth()));
//            closeNav.play();
//        });
        //Handle Mouse on ToolBar
        hbox.setOnMouseExited((MouseEvent event) -> {
            TranslateTransition closeNav = new TranslateTransition(new Duration(350), bar);
            closeNav.setToX(-(((GridPane) gridPane).getWidth()));
            closeNav.play();
        });

//        bar.setOnMouseEntered((MouseEvent event) -> {
//            TranslateTransition openNav = new TranslateTransition(new Duration(350), bar);
//            openNav.setToX(0);
//            if (bar.getTranslateX() != 0) {
//                openNav.play();
//            }
//        });

        Scene scene = new Scene(gridPane, 240, 100);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        Application.launch(args);
    }
}
guleryuz
  • 2,481
  • 1
  • 13
  • 17