I'm trying to get the html formatted content from my JTextPane
.
Problem is that when I insert the text with a specified AttributeSet
, the content text is not output when trying to write it out to a file, but the styling is.
I'm not sure if this is to do with how I am inserting the text or how I'm attempting to write it out to a file.
Here is an example:
import javax.swing.*;
import javax.swing.text.*;
import javax.swing.text.html.*;
import java.awt.BorderLayout;
import java.io.*;
import java.awt.Color;
public class SOExample extends JFrame
{
public static void main (String[] args)
{
SwingUtilities.invokeLater(
new Runnable()
{
@Override
public void run()
{
SOExample aFrame = new SOExample();
aFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
aFrame.setVisible(true);
}
}
);
}
public SOExample()
{
initComponents();
addText("This is my plain text", null);
SimpleAttributeSet BOLD = new SimpleAttributeSet();
StyleConstants.setBold(BOLD, true);
StyleConstants.setForeground(BOLD, Color.BLUE);
addText("This is my BLUE BOLD text",BOLD);
outputHTMLfile();
}
private void initComponents()
{
this.setBounds(300,300,300,300);
jtp = new JTextPane();
jtp.setContentType("text/html");
jsp = new JScrollPane();
JPanel jp = new JPanel(new BorderLayout());
jp.add(jtp);
jsp.add(jp);
jsp.setViewportView(jp);
this.add(jsp, BorderLayout.CENTER);
}
private void addText(String text, SimpleAttributeSet attr)
{
try
{
HTMLDocument doc = (HTMLDocument)jtp.getDocument();
doc.insertString(doc.getLength(), text +"\n", attr);
}
catch (BadLocationException blex)
{
blex.printStackTrace();
}
}
private void outputHTMLfile()
{
File f = new File("C:\\Temp", "TestFile.html");
try
{
BufferedOutputStream br = new BufferedOutputStream(new FileOutputStream(f));
HTMLEditorKit kit = new HTMLEditorKit();
kit.write(br, (HTMLDocument)jtp.getDocument(), 0, ((HTMLDocument)jtp.getDocument()).getLength() );
}
catch (Exception e)
{
e.printStackTrace();
}
}
private JTextPane jtp;
private JScrollPane jsp;
}
This will give me the output file with html like this
<html>
<head>
</head>
<body>
<p style="margin-top: 0">
This is my plain text
</p>
<p style="margin-top: 0">
<b><font color="#0000ff"><p>
</font></b> </p>
</body>
</html>
As you can see this is missing the text "This is my BLUE BOLD text"
but it will show correctly in the frame.
I've also tried writing jtp.getText()
directly to the file and get the same result.