JEditorPane and custom editor kit

前端 未结 3 1797
青春惊慌失措
青春惊慌失措 2021-01-27 01:44

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 for

相关标签:
3条回答
  • 2021-01-27 02:14

    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!

    0 讨论(0)
  • 2021-01-27 02:29

    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.

    0 讨论(0)
  • 2021-01-27 02:37

    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.

    0 讨论(0)
提交回复
热议问题