hello in my project,i use the JTextArea as the display interface to user and make it can't be edited. in this text area,i want some information are displayed differently in color from other info,but i don't know how to set the font color for these special info? who can help me? thank you!
In my experience : I can set font color in JTextPane Setting the Font and Color of Text in a JTextPane Using Styles A style is a set of text attributes, such as font size and color. A style can be applied any number of times to the contents of a text pane. When a style is applied to a word in the text pane, the style is not associated with the word. Rather, the contents of the style, namely the attributes, are associated with the word. This means that if the style is changed, the set of attributes associated with the word does not change. Styles can be stored in a text pane so that they can be retrieved, modified, and applied later. It is not necessary for a style to be stored with a text pane in order to use the style on the text pane. This example demonstrates the creation and application of styles in the contents of a JTextPane. See StyleConstants for a complete set of available attributes.
Java Developer, Thailand
JTextArea doesn't allow you to set, say, foreground color. The easiest 'visual/view' thing to do with JTextArea is highlight text (DefaultHighlighter.DefaultHighlightPainter ...). This just paints the background color - but may be what you need eg: <code> Highlighter hiliter = jta.getHighlighter(); DefaultHighlighter.DefaultHighlightPainter yellowHiLite = new DefaultHighlighter.DefaultHighlightPainter (Color.YELLOW); // highlight some text: hiliter.addHighlight (textStart, textEnd, yellowHiLite); </code> But previous response rightly noted that you probably want to use something like JTextPane (... likewise some JComponents like JLabel support HTML content ...)