New version is available You are viewing the documentation for JxBrowser 6 which is not supported since December 2019. Go to the current documentation.
List icon 目录

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.

Overview

By default spell checker is enable and configured to use English (en-US) language. Chromium engine checks text in all text fields and text areas on the loaded web page and highlights all misspelled words.

Language

JxBrowser provides API that allows enabling/disabling spell checker and configuring it to use specified language. The following code demonstrates how to enable spell checker functionality and configure it to use German (Standard) language (de):

Browser browser = new Browser();
SpellCheckerService spellChecker = browser.getContext().getSpellCheckerService();
spellChecker.setEnabled(true);
spellChecker.setLanguage("de");

Dictionaries

Chromium supports both custom dictionary and dictionaries for different languages. It downloads required dictionary for the current language automatically. You can also add words to your custom dictionary which is stored in Chromium user’s profile directory.

Events

When a text field or text area on the loaded web page receives focus, Chromium’s spell checker functionality automatically checks the text and highlights misspelled words. Using the SpellCheckListener you can obtain the information about misspelled words as well.

The following example demonstrates how to get notifications about spell check results:

SpellCheckerService spellCheckerService =
        browser.getContext().getSpellCheckerService();
spellCheckerService.addSpellCheckListener(new SpellCheckListener() {
    @Override
    public void onSpellCheckCompleted(SpellCheckCompletedParams params) {
        String text = params.getText();
        List<SpellCheckResult> checkResults = params.getResults();
        for (SpellCheckResult checkResult : checkResults) {
            int errorStartIndex = checkResult.getStartIndex();
            int errorLength = checkResult.getLength();
        }
    }
});

Context Menu

When user right clicks highlighted misspelled word on the loaded web page, context menu with suggestions can be displayed. JxBrowser provides API you can use to implement such context menu. The following example demonstrates how to register custom context menu handler that:

  • Displays suggestions according to the currently selected language. When user selects menu item with suggestion, the misspelled word is automatically replaced.
  • Displays the Add to Dictionary menu item. When user selects the menu item, the misspelled word is added to custom dictionary. The custom dictionary is stored in Chromium user’s profile directory.
import com.teamdev.jxbrowser.chromium.*;
import com.teamdev.jxbrowser.chromium.swing.BrowserView;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

/**
 * The sample demonstrates how to work with spellchecker API.
 */
public class SpellCheckerSample {
    public static void main(String[] args) throws Exception {
        // Enable heavyweight popup menu for heavyweight (default) BrowserView component.
        JPopupMenu.setDefaultLightWeightPopupEnabled(false);

        Browser browser = new Browser();
        BrowserView view = new BrowserView(browser);

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.add(view, BorderLayout.CENTER);
        frame.setSize(700, 500);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

        BrowserContext context = browser.getContext();
        SpellCheckerService spellCheckerService = context.getSpellCheckerService();
        // Enable SpellChecker service.
        spellCheckerService.setEnabled(true);
        // Configure SpellChecker's language.
        spellCheckerService.setLanguage("en-US");

        browser.setContextMenuHandler(new MyContextMenuHandler(view, browser));
        browser.loadHTML("<html><body><textarea rows='20' cols='30'>" +
                "Smple text with mitake.</textarea></body></html>");
    }

    private static class MyContextMenuHandler implements ContextMenuHandler {

        private final JComponent component;
        private final Browser browser;

        private MyContextMenuHandler(JComponent parentComponent, Browser browser) {
            this.component = parentComponent;
            this.browser = browser;
        }

        public void showContextMenu(final ContextMenuParams params) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    JPopupMenu popupMenu = createPopupMenu(params);
                    Point location = params.getLocation();
                    popupMenu.show(component, location.x, location.y);
                }
            });
        }

        private JPopupMenu createPopupMenu(final ContextMenuParams params) {
            final JPopupMenu result = new JPopupMenu();
            // Add suggestions menu items.
            List<String> suggestions = params.getDictionarySuggestions();
            for (final String suggestion : suggestions) {
                result.add(createMenuItem(suggestion, new Runnable() {
                    public void run() {
                        browser.replaceMisspelledWord(suggestion);
                    }
                }));
            }
            if (!suggestions.isEmpty()) {
                // Add the "Add to Dictionary" menu item.
                result.addSeparator();
                result.add(createMenuItem("Add to Dictionary", new Runnable() {
                    public void run() {
                        String misspelledWord = params.getMisspelledWord();
                        browser.addWordToSpellCheckerDictionary(misspelledWord);
                    }
                }));
            }
            return result;
        }

        private static JMenuItem createMenuItem(String title, final Runnable action) {
            JMenuItem result = new JMenuItem(title);
            result.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    action.run();
                }
            });
            return result;
        }
    }
}
Go Top