JTextPane problems when setting document

Discussion in 'Java' started by Michael Jung, Mar 1, 2008.

  1. Michael Jung

    Michael Jung Guest

    Hi,

    I don't know where the problem is with the following code:

    public static void main(String[] args) throws Exception {
    JFrame frame = new JFrame("Test");
    JTextPane showcase = new JTextPane();
    // showcase.setPage(new File("test.html").toURI().toURL());
    HTMLEditorKit kit = (HTMLEditorKit) showcase.getEditorKitForContentType("text/html");
    HTMLDocument doc = (HTMLDocument) kit.createDefaultDocument();
    doc.putProperty("IgnoreCharsetDirective", new Boolean(true));
    BufferedReader br = new BufferedReader(new FileReader(new File("test.html")));
    String in, txt = "";
    while ((in = br.readLine()) != null)
    txt += in;
    showcase.setStyledDocument(doc);
    kit.read(new StringReader(txt), doc, 0);

    frame.add(new JScrollPane(showcase));
    frame.pack();
    frame.setVisible(true);
    }

    This does not show the contents correctly. (HTML below renders as one
    rigid line.) When commenting out "HTMLEditorKit ... kit.read(...);"
    and uncommenting "showcase.setPage(...)" the contents are shown
    correctly. I have tried changing a few things around to no avail.
    The point is that the contents are actually delivered in a string, so
    I can't resort to setPage. Anybody have a clue? Sample html:

    <html>
    <head>
    <META http-equiv="Content-Type" content="text/html; charset=UTF-8">
    </head>
    <body>
    <table>
    <tr>

    <td>Person A</td>
    <td>Street A</td>
    <td>City A</td>
    <td>Country A</td>

    </tr>
    <tr>

    <td>Person B</td>
    <td>Street B</td>
    <td>City B</td>
    <td>Country B</td>

    </tr>
    </table>
    </body>
    </html>

    This is another nuissance: is there a way to get the parser to accept
    the Charset directive? I can either remove it or ignore it through
    properties.

    Michael
    Michael Jung, Mar 1, 2008
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. rj

    JTextPane columnar data

    rj, Sep 1, 2003, in forum: Java
    Replies:
    1
    Views:
    490
    Sandip Chitale
    Sep 1, 2003
  2. Adam
    Replies:
    0
    Views:
    436
  3. Sudarshan Raghavan

    JTextPane memory leak

    Sudarshan Raghavan, Oct 28, 2003, in forum: Java
    Replies:
    1
    Views:
    1,156
    Robert Olofsson
    Oct 29, 2003
  4. Alan Williams
    Replies:
    2
    Views:
    4,837
    Alan Williams
    Dec 16, 2003
  5. Fabrice
    Replies:
    4
    Views:
    17,377
    praitheesh
    Dec 31, 2008
Loading...

Share This Page