JTextPane problems when setting document

M

Michael Jung

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top