JavaFX HTMLEditor文本在5秒内更改
我遇到了一个问题,我很难找到解决办法。我得到了一个HtmlEditor事件,它可以检测何时按下一个键,但我需要创建一个侦听器来检查HtmlEditor文本是否已更改,我得出了以下结论:JavaFX HTMLEditor文本在5秒内更改,java,javafx,Java,Javafx,我遇到了一个问题,我很难找到解决办法。我得到了一个HtmlEditor事件,它可以检测何时按下一个键,但我需要创建一个侦听器来检查HtmlEditor文本是否已更改,我得出了以下结论: htmlEditor.setOnKeyPressed((keyEvent) ->{ if(keyEvent.getCode().isDigitKey() || keyEvent.getCode().isLetterKey() ||
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(),我想不出其他解决方案。提前谢谢。看来您需要对编辑的文本进行投票:
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();
}
}
});
Runnable textMonitor=new Runnable(){
私有字符串previousText;
@凌驾
公开募捐{
字符串newText=htmlEditor.getHtmlText();
布尔值更改=!newText.equals(上一个文本);
前文本=新文本;
如果(更改){
timer.playFromStart();
}
}
};
BooleanBinding windowShowing=Bindings.selectBoolean(
htmlEditor.sceneProperty(),“窗口”,“显示”);
windowShowing.addListener(
新的ChangeListener(){
专用ScheduledExecutorService执行器;
@凌驾
public void已更改(ObservaleValueRight,我明天会发短信给它。但是这里缺少一件事,我如何调用handleIsPaused()方法当这5秒结束时?你可以在任何地方调用timer.setOnFinished
。上面的代码只与启动动画有关。它不起作用。完成后,它应该转到另一个方法,但它永远不会进入其中。当它到达windowShowing.addListener时,它只会离开规则并转到每一行下面的一行还有别的事。