How can I set partial text color in JTextArea?

后端 未结 2 1143
挽巷
挽巷 2021-01-22 05:04

I want to set color for specific lines in the text area. What I\'ve found so far, is the following

// Declarations
private final DefaultStyledDocument document;         


        
相关标签:
2条回答
  • 2021-01-22 05:36

    Create a custom swing document extending for example PlainDocument and have a custom HighlightedView which is responsible for painting tokens.

    0 讨论(0)
  • 2021-01-22 05:40

    I'm not sure if JTextArea can be styled in so much detail, since it presumably sets up styles for the whole document from the selected font, color etc. You may have more luck using a JTextPane/JEditorPane.

    EDIT: From the javadoc

    A JTextArea is a multi-line area that displays plain text.

    (The emphasis is added.)

    If you can move to JTextPane, then that will render the formatting.

    0 讨论(0)
提交回复
热议问题