Note: Advice in this article will only work for JxBrowser 6. See the corresponding article for JxBrowser 7 here.(注意:本文中的建议仅适用于JxBrowser6,JxBrowser7相应文章请点击这里。)


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.(当加载的网页上的文本字段或文本区域获得焦点时,Chromium的拼写检查器功能会自动检查文本并突出显示拼写错误的单词。使用SpellCheckListener,您还可以获取有关拼写错误的单词的信息。)


The following example demonstrates how to get notifications about spell check results:(下面的示例演示如何获取有关拼写检查结果的通知:)

import com.teamdev.jxbrowser.chromium.*;
import com.teamdev.jxbrowser.chromium.swing.BrowserView;

import javax.swing.*;
import java.awt.*;
import java.util.List;

public class SpellCheckerSample {
    public static void main(String[] args) throws Exception {
        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);

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

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