Spell checker
This document shows how to configure languages for spell checking, add or remove words from a custom dictionary, disable spell checking, and more.
By default, spell checking is enabled. The spell checker analyzes text and highlights all misspelled words.
Spell checkers are associated with the profiles. To access and configure
the spell checker for the specific Profile use the SpellChecker class:
var spellChecker = profile.spellChecker();
val spellChecker = profile.spellChecker()
To disable spell checking, us the disable() method:
profile.spellChecker().disable();
profile.spellChecker().disable()
Another option is to use the
Engine.spellChecker()
method which returns the instance of the SpellChecker class associated with the default profile.
Unlike Windows and Linux, on macOS, Chromium uses spell checking settings and dictionaries provided by the OS. Hence, SpellChecker service behaves differently on macOS as you will see further in this guide.
Languages
The spell checker can check texts in different languages on a single web page. On Windows and Linux, Chromium automatically downloads the dictionary files from its servers and stores them in the user data directory. On macOS, Chromium uses dictionaries provided by the operating system.
The configuration for spelling languages, along with their dictionaries, is saved in
the user data. When you point Engine to a specific user data directory, it retains these
settings and reloads the corresponding dictionaries the next time it is created.
Use the SpellChecker.languages() method to list the languages used for spellchecking:
var languages = spellChecker.languages();
for (var language : languages) {
var code = language.code();
var region = language.region();
}
val languages = spellChecker.languages()
for (language in languages) {
val code = language.code()
val region = language.region()
}
Adding languages
Use the SpellChecker.addLanguage(Language) method to add a new spell checking language:
spellChecker.addLanguage(Language.GERMAN);
spellChecker.addLanguage(Language.of("en", "au"));
spellChecker.addLanguage(Language.GERMAN)
spellChecker.addLanguage(Language.of("en", "au"))
This method throws LanguageNotAvailableException if Chromium can’t find the necessary dictionary.
On macOS, this method only checks if the necessary dictionaries are available in the operating system.
Removing languages
On Windows and Linux, use the SpellChecker.removeLanguage(Language) method to stop spell checking for a particular
language:
spellChecker.removeLanguage(Language.GERMAN);
spellChecker.removeLanguage(Language.GERMAN)
On macOS, this method does nothing.
Custom dictionary
The spell checker supports the custom dictionary. You can access a custom dictionary using the
SpellChecker.customDictionary() method.
For example:
var dictionary = spellChecker.customDictionary();
val dictionary = spellChecker.customDictionary()
You can add words to the custom dictionary using the add(…) method, either individually or in bulk, which is faster for multiple words.
// Add one word:
var success = dictionary.add("John");
// Add many words at once:
var words = Set.of("JxBrowser", "TeamDev", "OFF_SCREEN");
List<SanitationError> errors = dictionary.add(words);
// Add one word:
val success = dictionary.add("John")
// Add many words at once:
val words = setOf("JxBrowser", "TeamDev", "OFF_SCREEN")
val errors = dictionary.add(words)
The word must be UTF-8, between 1 and 99 bytes long, and without leading or trailing ASCII whitespace.
To remove a word from the custom dictionary please, use the remove(String) method:
var success = dictionary.remove("John");
val success = dictionary.remove("John")
The check if the dictionary contains the word, use the has(String) method:
var isWordInDictionary = dictionary.has("John");
val isWordInDictionary = dictionary.has("John")
To list all words stored in the dictionary, use the words() method:
Collection<String> words = dictionary.words();
val words: Collection<String> = dictionary.words()
Disabling spell checking
By default, spell checking is enabled. To disable it use the following code:
spellChecker.disable();
spellChecker.disable()
Context menu
You can display a context menu with suggestions when a user right-clicks the highlighted misspelled word on the loaded
web page. Use the ShowContextMenuCallback callback to display a context menu with suggestions and a menu item that
allows adding the misspelled word to the custom dictionary.
While the mouse pointer is over the misspelled word, use Browser.replaceMisspelledWord(String) to replace it with
a suggestion:
browser.replaceMisspelledWord("Scrumdiddlyumptious");
browser.replaceMisspelledWord("Scrumdiddlyumptious")
See the guide on context menus for implementation examples for Swing, JavaFX, and SWT.
Spell checker events
When a text field or text area on the loaded web page receives focus, the spell checker automatically checks the text
and highlights the misspelled words. To get notifications when the text has been checked please use the
SpellCheckCompleted event.
For example:
browser.on(SpellCheckCompleted.class, event -> {
// The text that has been checked.
var text = event.checkedText();
// The list of the spell checking results.
event.results().forEach(spellCheckingResult -> {
// The location of the first symbol in the misspelled word
// in the checked text that is considered as misspelled
// by the spell checker.
var location = spellCheckingResult.location();
// The length of the misspelled word in the checked text.
var length = spellCheckingResult.length();
});
});
browser.subscribe<SpellCheckCompleted> { event ->
// The text that has been checked.
val text = event.checkedText()
// The list of the spell checking results.
event.results().forEach { spellCheckingResult ->
// The location of the first symbol in the misspelled word
// in the checked text that is considered as misspelled
// by the spell checker.
val location = spellCheckingResult.location()
// The length of the misspelled word in the checked text.
val length = spellCheckingResult.length()
}
}