This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

font color in JTextArea  RSS feed

 
zb cong
Ranch Hand
Posts: 416
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Surasak Leenapongpanit
Ranch Hand
Posts: 341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi zb cong.
You can use html code to seperate the different info.
 
somkiat puisungnoen
Ranch Hand
Posts: 1312
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
clio katz
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ...)
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!