Jxbrowser与tinymce和拼写检查

时间:2016-07-07 09:31:58

标签: swing tinymce spell-checking tinymce-4 jxbrowser

我在swing应用程序中使用jxbrowser作为嵌入式浏览器。 Jxbrowser可以选择拼写检查工作正常。

现在我必须使用像tinyMce这样的富文本编辑器,而spellCheck不能使用它。

我怎么能这样做spellCheck可以在jxbrowser中使用tinyMCe? java类:

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();
    spellCheckerService.addSpellCheckListener(new SpellCheckListener() {
        @Override
        public void onSpellCheckCompleted(SpellCheckCompletedParams params) {
            String text = params.getText();
            System.out.println(params.getResults().size());
            System.out.println("text = " + text);
            List<SpellCheckResult> mistakes = params.getResults();
            for (SpellCheckResult mistake : mistakes) {
                System.out.println("mistake.getStartIndex() = " + mistake.getStartIndex());
                System.out.println("mistake.getLength() = " + mistake.getLength());
            }
        }
    });
    // Enable SpellChecker service.
    spellCheckerService.setEnabled(true);
    // Configure SpellChecker's language.
    spellCheckerService.setLanguage("en-US");

    browser.setContextMenuHandler(new MyContextMenuHandler(view, browser));
    //browser.loadHTML(loadHtml);

    browser.loadURL("C:\\tiny.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;
    }
}

}

tiny.html:

<!DOCTYPE html>
<html>
<head>
   <script type="text/javascript" src="tinymce/tinymce.min.js"></script>
  <script>tinymce.init({ selector:'textarea' });</script>
</head>
<body>
  <textarea>Test eror</textarea>
</body>
</html>

1 个答案:

答案 0 :(得分:1)

解决方案是使用browser_spellcheck初始化tinyMCe:true

  <script>tinymce.init({
       selector:'textarea' ,
       browser_spellcheck: true,
       contextmenu: false
    });</script>