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


Note: By default, context menu is disabled.(注意:默认情况下,上下文菜单是禁用的。)


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:(当用户右键单击加载的网页上突出显示的拼写错误的单词时,可以显示带有建议的上下文菜单。 JxBrowser提供了可用于实现此类上下文菜单的API。下面的示例演示如何注册自定义上下文菜单处理程序:)


  • Displays suggestions according to the currently selected language. When user selects menu item with suggestion, the misspelled word is automatically replaced.(根据当前选择的语言显示建议。当用户选择带有建议的菜单项时,拼写错误的单词将被自动替换。)
  • Displays 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.(显示“添加到字典”菜单项。当用户选择菜单项时,拼写错误的单词将添加到自定义词典中。自定义词典存储在Chromium用户的个人资料目录中。)

 

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