JEdi​​torPane和自定义编辑器套件

我有一个小问题。 我需要使用自定义编辑器工具包将现有文件加载到JEditorPane 。 我有一个编辑器工具包,一个扩展名的文件,我需要强制JEditorPane识别我的文件并使用我的编辑器工具包。 我发现只有,这是可能的,但无处可去。

该工具包也基于HTML和文件。 如果file具有.html扩展名,则它可以工作,但是当我将文件重命名为.xhtbm ,它将以纯文本forms打开。 内容类型设置为text/plain ,但我无法为此类型注册我的编辑器工具包,因为已经为此内容类型注册了另一个编辑器工具包。

实际上问题是:是否真的可以将某些编辑器工具包与某种文件类型相关联?

设置您的EditorKit并使用工具包的read()方法将文件传递到那里。

read方法中使用的reader应该理解如何解析内容。

非常感谢斯坦尼斯拉夫。 在他的例子中(参见文章的最后一页 ,方法initEditor() ),我找到了正确的方法。 错误是命令的顺序。 这样可行:

 public void openFile(String fileName) throws IOException { editor.setEditorKit(new ModifiedHTMLEditorKit()); ModifiedHTMLDocument doc = (ModifiedHTMLDocument)editor.getDocument(); try { editor.getEditorKit().read(new FileReader(fileName), doc, 0); } catch (BadLocationException b) { throw new IOException("Could not fill data into editor.", b); } } 

然后我调用openFile("test.xhtbm") ,一切都没有摩擦。

你可以:

 static{ // register EditorKit for plaintext content JEditorPane.registerEditorKitForContentType( "text/plain", "HtmlEditorKit" ); } 

在你之前:

public static void main(String[] args){...}

回复晚了非常抱歉!