Swing 带有tinymce和拼写检查的Jxbrowser
我在swing应用程序中使用jxbrowser作为嵌入式浏览器。Jxbrowser有拼写检查选项,可以正常工作 现在我必须使用像tinyMce这样的富文本编辑器,拼写检查不能使用它 如何在jxbrowser中使用tinyMCe进行拼写检查? java类:Swing 带有tinymce和拼写检查的Jxbrowser,swing,tinymce,spell-checking,tinymce-4,jxbrowser,Swing,Tinymce,Spell Checking,Tinymce 4,Jxbrowser,我在swing应用程序中使用jxbrowser作为嵌入式浏览器。Jxbrowser有拼写检查选项,可以正常工作 现在我必须使用像tinyMce这样的富文本编辑器,拼写检查不能使用它 如何在jxbrowser中使用tinyMCe进行拼写检查? java类: public class SpellCheckerSample { public static void main(String[] args) throws Exception { // Enable heavyweight pop
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;
}
}
}
公共类拼写检查示例{
公共静态void main(字符串[]args)引发异常{
//为重量级(默认)浏览查看组件启用重量级弹出菜单。
jpopMenu.setDefaultLightWeightPopupEnabled(false);
浏览器=新浏览器();
BrowserView视图=新建BrowserView(浏览器);
JFrame=新JFrame();
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
添加(视图、边框布局、中心);
框架设置尺寸(700500);
frame.setLocationRelativeTo(空);
frame.setVisible(true);
BrowserContext=browser.getContext();
SpellCheckerService SpellCheckerService=context.getSpellCheckerService();
addSpellCheckListener(新建SpellCheckListener()){
@凌驾
public void onSpellCheckCompleted已完成(SpellCheckCompletedParams参数){
String text=params.getText();
System.out.println(params.getResults().size());
System.out.println(“text=“+text”);
List errors=params.getResults();
for(拼写检查结果错误:错误){
System.out.println(“error.getStartIndex()=”+error.getStartIndex());
System.out.println(“error.getLength()=”+error.getLength());
}
}
});
//启用拼写检查服务。
spellCheckerService.setEnabled(true);
//配置拼写检查器的语言。
spellCheckerService.setLanguage(“en-US”);
setContextMenuHandler(新的MyContextMenuHandler(视图,浏览器));
//loadHTML(loadHTML);
browser.loadURL(“C:\\tiny.html”);
}
私有静态类MyContextMenuHandler实现ContextMenuHandler{
私有最终组件;
专用最终浏览器;
私有MyContextMenuHandler(JComponent parentComponent,浏览器){
this.component=parentComponent;
this.browser=浏览器;
}
public void showContextMenu(最终ContextMenuParams参数){
SwingUtilities.invokeLater(新的Runnable(){
公开募捐{
JPopupMenu-popupMenu=createPopupMenu(参数);
点位置=params.getLocation();
显示(组件、位置.x、位置.y);
}
});
}
私有JPOppMenu createPopupMenu(最终ContextMenuParams参数){
最终JPopupMenu结果=新的JPopupMenu();
//添加建议菜单项。
List suggestions=params.getDictionarysSuggestions();
for(最终字符串建议:建议){
添加(createMenuItem(建议,新建Runnable()){
公开募捐{
浏览器。替换拼写错误的单词(建议);
}
}));
}
如果(!suggestions.isEmpty()){
//添加“添加到字典”菜单项。
result.addSeparator();
add(createMenuItem(“添加到字典”,new Runnable()){
公开募捐{
字符串拼写错误的单词=params.getMisspelledWord();
browser.addWordToSpellCheckerDictionary(拼写错误的单词);
}
}));
}
返回结果;
}
私有静态JMenuItem createMenuItem(字符串标题,最终可运行操作){
JMenuItem结果=新的JMenuItem(标题);
result.addActionListener(新ActionListener(){
已执行的公共无效操作(操作事件e){
action.run();
}
});
返回结果;
}
}
}
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>
init({选择器:'textarea'});
试验室
解决方案是使用浏览器初始化tinyMCe\u拼写检查:true
<script>tinymce.init({
selector:'textarea' ,
browser_spellcheck: true,
contextmenu: false
});</script>
tinymce.init({
选择器:'textarea',
浏览器拼写检查:true,
上下文菜单:false
});
它在谷歌Chrome中工作吗?我的意思是,如果它在Google Chrome中不起作用,那么它在Chrome中可能是“按设计”的行为。在Google Chrome中,它也以同样的方式起作用。解决方案是打开浏览器\u拼写检查:浏览器\u拼写检查:true,