Win a copy of Microservices Testing (Live Project) this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

font color in JTextArea

 
Ranch Hand
Posts: 416
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Ranch Hand
Posts: 341
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi zb cong.
You can use html code to seperate the different info.
 
Ranch Hand
Posts: 1312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ...)
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic