Line wrapping should normally occur when inserting a document in a
JTextPane, right?
Well for some reason it doesn't happen when inserting an HTML document
read from a File. If you execute the code below, the content of the
HTML page is displayed in a single line. Embedding the JTextPane inside
a JScrollPane didn't change a thing.
public class TextComponentDemo extends JFrame {
JTextPane textPane;
public TextComponentDemo() {
textPane = new JTextPane();
try {
HTMLEditorKit editorKit = new HTMLEditorKit();
HTMLDocument HTMLDoc =
(HTMLDocument)editorKit.createDefaultDocument();
FileReader in = new FileReader("simple.html"); // Simple HTML
file with normal English text
editorKit.read(in, HTMLDoc, 0);
textPane.setPreferredSize(new Dimension(200, 200));
textPane.setMaximumSize(new Dimension(200, 200));
textPane.setSize(new Dimension(200, 200)); // setting all those
size attributes didn't change anything :(
textPane.setDocument(HTMLDoc);}
catch(Exception e) {e.printStackTrace();}
JScrollPane scrollPane = new JScrollPane(textPane);
scrollPane.setPreferredSize(new Dimension(200, 200));
getContentPane().add(scrollPane);
}
private static void createAndShowGUI() {
final TextComponentDemo frame = new TextComponentDemo();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
---
Any idea why this happens?
Roland - 04 Feb 2005 12:53 GMT
> Line wrapping should normally occur when inserting a document in a
> JTextPane, right?
[quoted text clipped - 48 lines]
>
> Any idea why this happens?
Use
textPane.setContentType("text/html");
just before
textPane.setDocument(HTMLDoc);
However, instead of creating a HTMLDocument yourself, it's probably
easier and more reliable to use setPage(URL). The try block would then
have to be
try {
textPane.setContentType("text/html");
textPane.setPage(new File("simple.html").toURL());
} catch (... etc ...)
I believe that you even don't need setContentType(...);

Signature
Regards,
Roland de Ruiter
___ ___
/__/ w_/ /__/
/ \ /_/ / \
fahruz@hotmail.com - 07 Feb 2005 01:38 GMT
Great! With the setContentType("text/html") it worked fine. Thanks a
lot!
As for your other tip unfortunately I cannot use it within my
application because my program is in fact a tad more complicated (I
produced a sample code here with what I thought to be the core of the
problem, for the sake of explanation). Actually I have a Document that
is passed via a TransferHandler to the JTextPane. This Document can be
any subclass of Document, including but not limited to HTMLDocument.
However I guess it's ok if I include a "(if Document instanceof
HTMLDocument) textPane.setContentType("text/html");" in my class
without adding to much specificity.
Many thanks again.
F.