在TextArea中拼写检查文本

在TextArea中拼写检查文本

问题描述:

如何拼写检查从用户输入的文本到TextArea?

How I can spell check text typed from the user into TextArea?

这个JavaFX组件可以实现吗?

Is this possible with this JavaFX component?

我可以使用Java for JavaFX的标准拼写检查器吗?

Can I use standard spellchecker from Java for JavaFX?

你可以使用 CodeArea 突出显示错误。

You can use CodeArea to highlight the errors.

CodeArea codeArea = new CodeArea();
codeArea.textProperty().addListener((observable, oldText, newText) -> {
    List<IndexRange> errors = spellCheck(newText);
    for(IndexRange error: errors) {
        codeArea.setStyleClass(error.getStart(), error.getEnd(), "spell-error");
    }
});

List<IndexRange> spellCheck(String text) {
    // Implement your spell-checking here.
}

此外,在样式表中设置错误样式

In addition, set the error style in your stylesheet

.spell-error {
    -fx-effect: dropshadow(gaussian, red, 2, 0, 0, 0);
}

请注意,您需要JDK8才能使用CodeArea。

Note that you need JDK8 to use CodeArea.