4

I am developing a JavaFX application where I am using java.util.Timer to track user mouse movement.

The concept is If the mouse doesn't move for some seconds in the scene then the buttons will be invisible whenever the mouse moves button will appear again. And whenever user put the cursor over a button timer will be stopped.And when exit the button timer will start again.

This is the start timer method

    public static void startTimer(){

    timer = new Timer();
    task = new TimerTask() {

        @Override
        public void run() {

            if(detection>0){
                Util_Class.getUi_obj().getLeftbuttongroup().setVisible(false);
                Util_Class.getUi_obj().getRightbuttongroup().setVisible(false);
            }else{
                detection++;
            }

        }
    };

    timer.schedule(task, 2000, 2000);
    System.out.println("TIMER STARTED");
    //startTimer();
}

This is to stop the timer

public static void stopTimer(){

    timer.cancel();
    System.out.println("TIMER STOPED");
}



public void leftbuttonmovehandler(MouseEvent event){


    if(event.getEventType()==MouseEvent.MOUSE_ENTERED){
        System.out.println("MOUSE ENTERED");
        Main.stopTimer();
    }else if(event.getEventType()==MouseEvent.MOUSE_EXITED){
        System.out.println("MOUSE EXITED");
        Main.start();
    }

}

Now my code works fine at first whenever the app starts but when i reload the app the callback functions triggers as expected but the buttons disappear despite the cursor is on the button.

It would be great if someone helps me.

  • 1
    There are much simpler ways to achieve this in JavaFX using a `PauseTransition`; do you want to see that solution instead? – James_D Sep 02 '16 at 16:39
  • yes off course.I want to see that. – Saidul Amin Nobin Sep 02 '16 at 16:42
  • Was just trying to find it: see [this question](http://stackoverflow.com/questions/33066754/javafx-set-mouse-hidden-when-idle) which sets the cursor to `NONE`; but you can equally set the buttons visibility to `false`. – James_D Sep 02 '16 at 16:43

1 Answers1

2

A PauseTransition can be used to hide the Buttons after a specified time.

Whenever a MOUSE_MOVE event is triggered, on the scene, play the PauseTransition from the beginning ; if the MOUSE_MOVE is triggered for one of the Buttons stop the PauseTransition instead and prevent the event from reaching the Scene by consuming it

private PauseTransition timer;

private void startTimer() {
    btn.setVisible(true);
    btn2.setVisible(true);
    timer.playFromStart();
}

private void stopTimer() {
    btn.setVisible(true);
    btn2.setVisible(true);
    timer.stop();
}

private Button btn, btn2;

@Override
public void start(Stage primaryStage) {
    timer = new PauseTransition(Duration.seconds(3));

    btn = new Button("Button 1");
    btn2 = new Button("Button 2");

    timer.setOnFinished(evt -> {
        btn.setVisible(false);
        btn2.setVisible(false);
    });

    EventHandler<MouseEvent> buttonMouseMoveHandler = evt -> {
        evt.consume();
        stopTimer();
    };

    btn.setOnMouseMoved(buttonMouseMoveHandler);
    btn2.setOnMouseMoved(buttonMouseMoveHandler);

    VBox box = new VBox(100, btn, btn2);

    StackPane root = new StackPane(new Group(box));

    Scene scene = new Scene(root, 500, 500);
    scene.setOnMouseMoved(evt -> {
        startTimer();
    });

    startTimer();

    primaryStage.setScene(scene);
    primaryStage.show();
}
fabian
  • 80,457
  • 12
  • 86
  • 114