简体   繁体   中英

JavaFX HTMLEditor text changed in 5 seconds

I've come up with a problem that I'm having trouble finding out how to solve. I got a HtmlEditor event which detects when a key is pressed, but I need to create a listener to check if the HtmlEditor text was changed, and I came up with this:

htmlEditor.setOnKeyPressed((keyEvent) ->{
                            if(keyEvent.getCode().isDigitKey() || keyEvent.getCode().isLetterKey() ||
                            keyEvent.getCode().isModifierKey() || keyEvent.getCode().isWhitespaceKey() ||
                            keyEvent.isShiftDown()){
                                handleWhoTypes(null);     
                            }  
                        });

                        PauseTransition timer = new PauseTransition(Duration.seconds(5));
                        htmlEditor.textProperty().addListener((obs, oldText, newText) ->
                            timer.playFromStart());
                        timer.setOnFinished(e -> handleIsPaused(null));
        }

But this won't work because the the htmlEditor doesn't have textProperty() and I cannot think of another solution. Thank you in advance.

Looks like you'll have to poll the editor's text:

Runnable textMonitor = new Runnable() {
    private String previousText;

    @Override
    public void run() {
        String newText = htmlEditor.getHtmlText();
        boolean changed = !newText.equals(previousText);

        previousText = newText;

        if (changed) {
            timer.playFromStart();
        }
    }
};

BooleanBinding windowShowing = Bindings.selectBoolean(
    htmlEditor.sceneProperty(), "window", "showing");
windowShowing.addListener(
    new ChangeListener<Boolean>() {
        private ScheduledExecutorService executor;

        @Override
        public void changed(ObservableValue<? extends Boolean> o,
                            Boolean old,
                            Boolean showing) {
            if (showing) {
                executor = Executors.newSingleThreadScheduledExecutor();
                executor.scheduleWithFixedDelay(
                    () -> Platform.runLater(textMonitor),
                    1, 1, TimeUnit.SECONDS);
            } else {
                executor.shutdown();
            }
        }
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM