我遇到了一个问题,我无法找到解决方法。我有一个HtmlEditor事件,它检测按键何时被按下,但是我需要创建一个监听器来检查HtmlEditor文本是否被更改,我想出了这个:
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));
}
但这不起作用,因为htmlEditor没有textProperty(),我想不出另一个解决方案。提前谢谢。
答案 0 :(得分:1)
看起来你必须轮询编辑的文字:
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();
}
}
});