Reputation: 58
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.
Upvotes: 0
Views: 409
Reputation: 44345
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();
}
}
});
Upvotes: 1