How to display bold text in only parts of JTextArea?

后端 未结 3 490
粉色の甜心
粉色の甜心 2020-12-03 12:19

Can I alter the text of a JTextArea to bold (append text) and then back to normal and will it only display the bold text in bold and the rest as normal?

相关标签:
3条回答
  • 2020-12-03 12:54

    I believe you need a JTextPane or JEditorPane for that.

    0 讨论(0)
  • 2020-12-03 13:09

    No. What you're looking for is JEditorPane

    This supports HTML (3.2?) which will allow you to use <font> (and other older tags) to provide rich text.

    JEditorPane textarea = new JEditorPane("text/html", "");
    textarea.setText("Here is some <b>bold text</b>");
    

    EDIT: According to the javadoc I referenced above, JEditorPane also supports limited RTF. Don't forget to change the MIME to text/rtf

    0 讨论(0)
  • 2020-12-03 13:19
     textArea.setFont(textArea.getFont().deriveFont(Font.BOLD, textArea.getFont().getSize()));
    
    0 讨论(0)
提交回复
热议问题