3

I have a JTextPane, and I would like to output text in it using StyledDocument. Here is my StyledDocument object:

StyledDocument dox = (StyledDocument) textArea.getDocument();

Style style = dox.addStyle("StyleName", null);

StyleConstants.setFontFamily(style, Font.SANS_SERIF);
StyleConstants.setFontSize(style, 8);
dox.insertString(dox.getLength(), "<b>Some Text</b>", null);

The problem right now is if I edit the text with html code, it does not display the way I want. I want the text to be displayed as bolded instead of literally <b>Some Text</b>.

Is there a way to do this?

user11809641
  • 815
  • 1
  • 11
  • 22
livelaughlove
  • 376
  • 3
  • 9
  • 21

1 Answers1

2

I did figure it out on my own in the end using HTMLEditorKit, here's the answer for futher reference

    StyledDocument dox = (StyledDocument) textArea.getDocument();
    textPane.setEditorKit(new HTMLEditorKit());

    textPane.setText("<b>Some Text</b>");
Community
  • 1
  • 1
livelaughlove
  • 376
  • 3
  • 9
  • 21
  • `textArea` and `textPane`. No actual apparent use of defined style. – Philip Whitehouse Dec 22 '12 at 13:59
  • 2
    you don't need any StyledDocument for this at all. The given solution just re-parses the whole JTextPane content. // PS. ".setEditorKit(editorKit)" could also be .setContentType("text/html"); – phil294 Dec 16 '14 at 19:21