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 SpellCheckService class:

SpellCheckService spellCheckerService = engine.getSpellCheckerService();

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 SpellCheckService.setDictionaries(String...) method. Chromium downloads the dictionary files automatically from its web servers.

For example:

spellCheckerService.setDictionaries("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 SpellCheckService.getCustomDictionary() method.

For example:

SpellCheckDictionary customDictionary = spellCheckerService.getCustomDictionary();

Adding Word

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

boolean success = customDictionary.addWord("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 removeWord(String) method:

boolean success = customDictionary.removeWord("John");

Disabling Spell Checking

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

spellCheckerService.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, callback) -> {
    SwingUtilities.invokeLater(() -> {
        JPopupMenu popupMenu = new JPopupMenu();

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

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

        // Add the "Add to Dictionary" menu item.
        JMenuItem addToDictionary = new JMenuItem("Add to Dictionary");
        addToDictionary.addActionListener(e -> {
            SpellCheckDictionary customDictionary =
                    engine.getSpellCheckerService().getCustomDictionary();
            customDictionary.addWord(params.getMisspelledWord());
        });
        popupMenu.add(addToDictionary);

        // Display context menu at specified location.
        Point location = params.getLocation();
        popupMenu.show(view, location.getX(), location.getY());
    });
    callback.proceed();
});

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, callback) -> {
    Platform.runLater(() -> {
        ContextMenu contextMenu = new ContextMenu();

        // Add the suggestions menu items.
        List<String> suggestions = params.getDictionarySuggestionsList();
        suggestions.forEach(suggestion -> {
            MenuItem item = new MenuItem(suggestion);
            item.setOnAction(event ->
                    browser.replaceMisspelledWord(suggestion));
            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("Add to Dictionary");
        addToDictionary.setOnAction(event -> {
            SpellCheckDictionary customDictionary =
                    engine.getSpellCheckerService().getCustomDictionary();
            customDictionary.addWord(params.getMisspelledWord());
        });
        contextMenu.getItems().add(addToDictionary);

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

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.getText();
    // The list of the spell checking results.
    List<Result> results = event.getResultsList();
    for (Result result : results) {
        // The index of the first symbol in the substring of
        // the checked text which is considered as mis-spelled.
        int start = result.getStart();
        // The index of the last symbol in the substring of
        // the checked text which is considered as mis-spelled.
        int end = result.getEnd();
    }
});
Go Top