Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to output using StyledDocument with HTML?

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?

like image 728
livelaughlove Avatar asked Apr 09 '26 09:04

livelaughlove


1 Answers

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>");
like image 120
livelaughlove Avatar answered Apr 10 '26 23:04

livelaughlove



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!