This documents shows how to configure spell checking with the required dictionaries, add or remove words from a custom dictionary, disable spell checking and more.

By default spell checking is enabled and configured to use the en-US dictionary. Spell checker checks the text in all text fields and text areas on the loaded web page and highlights all misspelled words. To access and configure the spell checker use the SpellChecker class:

SpellChecker spellChecker = engine.spellChecker();

Dictionaries

The spell checker supports multiple dictionaries and can check the text in different languages on a single web page. To configure the spell checker with the required dictionaries please use the SpellChecker.dictionaryNames(String...) method. Chromium downloads the dictionary files automatically from its web servers.

For example:

spellChecker.dictionaryNames("en-US", "de");

The code above configures the spell checker with the German de and English en-US dictionaries.

Custom Dictionary

The spell checker supports the custom dictionary. You can access a custom dictionary using the SpellChecker.getCustomDictionary() method.

For example:

Dictionary dictionary = spellChecker.customDictionary();

Adding Word

You can add words to the custom dictionary using the add(String) method:

boolean success = dictionary.add("John");

The word must be UTF-8, between 1 and 99 bytes long, and without leading or trailing ASCII whitespace.

Removing Word

To remove a word from the custom dictionary please use the remove(String) method:

boolean success = dictionary.remove("John");

Disabling Spell Checking

By default spell checking is enabled. To disable spell checking use the following code:

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.

Swing

The following example demonstrates how to create and display Swing context menu with suggestions and the Add to Dictionary menu item. Using this context menu, you can replace the misspelled word with one of the suggestions or add it to the custom dictionary.

browser.set(ShowContextMenuCallback.class, (params, tell) ->
        SwingUtilities.invokeLater(() -> {
            JPopupMenu popupMenu = new JPopupMenu();
            popupMenu.addPopupMenuListener(new PopupMenuListener() {
                ...
                @Override
                public void popupMenuCanceled(PopupMenuEvent e) {
                    tell.close();
                }
            });

            // Add the suggestions menu items.
            SpellCheckMenu spellCheckMenu = params.spellCheckMenu();
            List<String> suggestions = spellCheckMenu.dictionarySuggestions();
            suggestions.forEach(suggestion -> {
                JMenuItem menuItem = new JMenuItem(suggestion);
                menuItem.addActionListener(e -> {
                    browser.replaceMisspelledWord(suggestion);
                    tell.close();
                });
                popupMenu.add(menuItem);
            });

            // Add menu separator if necessary.
            if (!suggestions.isEmpty()) {
                popupMenu.addSeparator();
            }

            // Add the "Add to Dictionary" menu item.
            JMenuItem addToDictionary = new JMenuItem(
                    spellCheckMenu.addToDictionaryMenuItemText());
            addToDictionary.addActionListener(e -> {
                Dictionary dictionary =
                        engine.spellChecker().customDictionary();
                dictionary.add(spellCheckMenu.misspelledWord());
                tell.close();
            });
            popupMenu.add(addToDictionary);

            // Display context menu at specified location.
            Point location = params.location();
            popupMenu.show(view, location.x(), location.y());
        }));

If you right click on a misspelled word, the following context menu will be displayed: Swing Spell Checker Context Menu

JavaFX

The following example demonstrates how to create and display a JavaFX context menu with suggestions and the Add to Dictionary menu item. Using this context menu, you can replace the misspelled word with one of the suggestions or add it to the custom dictionary.

browser.set(ShowContextMenuCallback.class, (params, tell) ->
        Platform.runLater(() -> {
            ContextMenu contextMenu = new ContextMenu();
            contextMenu.setAutoHide(true);
            contextMenu.setOnHidden(event -> {
                if (!tell.isClosed()) {
                    tell.close();
                }
            });
            view.setOnMousePressed(event -> {
                if (contextMenu.isShowing()) {
                    contextMenu.hide();
                }
            });
            // Add the suggestions menu items.
            SpellCheckMenu spellCheckMenu = params.spellCheckMenu();
            List<String> suggestions = spellCheckMenu.dictionarySuggestions();
            suggestions.forEach(suggestion -> {
                MenuItem item = new MenuItem(suggestion);
                item.setOnAction(event -> {
                    browser.replaceMisspelledWord(suggestion);
                    tell.close();
                });
                contextMenu.getItems().add(item);
            });

            // Add menu separator if necessary.
            if (!suggestions.isEmpty()) {
                contextMenu.getItems().add(new SeparatorMenuItem());
            }

            // Add the "Add to Dictionary" menu item.
            MenuItem addToDictionary = new MenuItem(
                    spellCheckMenu.addToDictionaryMenuItemText());
            addToDictionary.setOnAction(event -> {
                SpellChecker spellChecker = engine.spellChecker();
                Dictionary dictionary = spellChecker.customDictionary();
                dictionary.add(spellCheckMenu.misspelledWord());
                tell.close();
            });
            contextMenu.getItems().add(addToDictionary);

            // Display context menu at specified location on screen.
            Point location = params.location();
            Point2D locationOnScreen = 
                    view.localToScreen(location.x(), location.y());
            contextMenu.show(view, locationOnScreen.getX(), 
                    locationOnScreen.getY());
        }));

If you right click on a misspelled word, the following context menu will be displayed: JavaFX Spell Checker Context Menu

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.
    String text = event.checkedText();
    // The list of the spell checking results.
    event.results().forEach(spellCheckingResult -> {
        // The location of the first symbol in the mis-spelled word 
        // in the checked text that is considered as mis-spelled 
        // by the spell checker.
        int location = spellCheckingResult.location();
        // The length of the mis-spelled word in the checked text
        int length = spellCheckingResult.length();
    });
});
Go Top