JEditorPane and custom editor kit

醉酒当歌 提交于 2019-12-20 04:52:19

问题


I've a trivial question. I need to load an existing file into JEditorPane using custom editor kit. I've a editor kit, a file with some extension and I need to force the JEditorPane to recognize my file and use my editor kit. I've found only, that's possibile, but nowhere how.

The kit is based on HTML and the file too. If file has the .html extension, it works, but when I rename the file to .xhtbm, it is opened as plain text. The content type is set to text/plain, but I'm unable to register my editor kit for this type, because there is already registered another editor kit for this content type.

Actually the question is: Is really possible to associate some editor kit with some file type?


回答1:


Set your EditorKit and user the kit's read() method passing the file there.

The reader used in the read method should understand how to parse the content.




回答2:


Thanks a lot Stanislav. In his example (see the last page of article, method initEditor()) I found the proper way. The mistake was in the order of commands. That works:

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

Then I call openFile("test.xhtbm") and all goes without friction.




回答3:


You Could:

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

before your:

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

Sorry for the Late Response!



来源:https://stackoverflow.com/questions/7526883/jeditorpane-and-custom-editor-kit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!