JavaFX 8: how to add a timedelay to a listener?

前端 未结 2 1947
故里飘歌
故里飘歌 2020-12-06 21:46

I\'m working on an JavaFX 8 app right now, where i have a tableView and some textFields above which make it possible to search/filter for certain columns in the tableView. I

2条回答
  •  無奈伤痛
    2020-12-06 22:27

    The code below will schedule to do something after a 1 second delay from the last time a text field changes. If the text field changes within that 1 second window, the previous change is ignored and the something is scheduled to be done with the new value 1 second from the most recent change.

    PauseTransition pause = new PauseTransition(Duration.seconds(1));
    textField.textProperty().addListener(
        (observable, oldValue, newValue) -> {
            pause.setOnFinished(event -> doSomething(newValue));
            pause.playFromStart();
        }
    );
    

    I didn't test this, but it should work :-)

    A more sophisticated solution might be to make use of a "forgetful" ReactFX suspendable event stream. ReactFX based solutions are discussed in the related question:

    • Wait before Reacting to a Property Change JavaFX 8

提交回复
热议问题