Spell check text in TextArea

2019-02-23 01:06发布

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

Is this possible with this JavaFX component?

Can I use standard spellchecker from Java for JavaFX?

1条回答
霸刀☆藐视天下
2楼-- · 2019-02-23 01:49

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);
}

Note that you need JDK8 to use CodeArea.

查看更多
登录 后发表回答