Mam sobie taki prosty kod:
public class Main {
public static void main(String[] args) {
JFrame f = new JFrame("HTML test");
f.setSize(400, 400);
JTextPane p = new JTextPane();
p.setContentType("text/html");
p.setText("<html><head></head><body><p>xxx you yyy</p></body></html>");
f.getContentPane().add(p);
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
HTMLDocument doc = (HTMLDocument) p.getDocument();
SimpleAttributeSet a = new SimpleAttributeSet();
a.addAttribute(HTML.Attribute.COLOR, "#ff0000");
a.addAttribute(StyleConstants.NameAttribute, HTML.Tag.FONT);
doc.setCharacterAttributes(5, 3, a, false);
String text = p.getText();
System.out.print(text);
}
}
Jak widzicie jako dane wejściowe mam HTML:
<html><head></head><body><p>xxx you yyy</p></body></html>
któremu chce ustawić frazę you na czerwono ale zamiast tego otrzymuje
<html>
<head>
</head>
<body>
<p>
xxx <font color="#ff0000"> yyy
</p>
</body>
</html>
Czy ktoś wie może jak to poprawnie zrobić z obiektem HTMLDocument ?
Z góry dzięki za pomoc.