开发者

How to display bold text in only parts of JTextArea?

开发者 https://www.devze.com 2022-12-28 00:13 出处:网络
Can I alter the text of a JTextArea to bold (append text) and then back to normal and will it o开发者_运维百科nly display the bold text in bold and the rest as normal?

Can I alter the text of a JTextArea to bold (append text) and then back to normal and will it o开发者_运维百科nly display the bold text in bold and the rest as normal?

Also can the contents of JTextArea be saved as an RTF document?


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


 textArea.setFont(textArea.getFont().deriveFont(Font.BOLD, textArea.getFont().getSize()));


I believe you need a JTextPane or JEditorPane for that.

0

精彩评论

暂无评论...
验证码 换一张
取 消