• Post Reply Bookmark Topic Watch Topic
  • New Topic

Multicolored JTextPane/ JEditorPane/ JTextArea output  RSS feed

 
Jack Conger
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hiya,
I'm making a program that utilizes a frequently updated multi lined text component, which is currently not in color. And I'd like to make it in color. I've tried to read stuff on JEditorPane and using setContentType() and all that, and a little JTextPane stuff, but all of it makes my head spin. Any help would be appreciated.

P.S. The JTextArea I'm using uses a lot of append()s.
 
Rob Spoor
Sheriff
Posts: 20898
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could check out javax.swing.text.Highlighter, particularly javax.swing.text.DefaultHighlighter and javax.swing.text.DefaultHighlighter.DefaultHighlightPainter:

This code will paint the first 15 characters of a JTextComponent called text in read.
 
Brian Cole
Author
Ranch Hand
Posts: 936
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jack Conger:
I'm making a program that utilizes a frequently updated multi lined text component, which is currently not in color. And I'd like to make it in color. I've tried to read stuff on JEditorPane and using setContentType() and all that, and a little JTextPane stuff, but all of it makes my head spin. Any help would be appreciated.

P.S. The JTextArea I'm using uses a lot of append()s.


It's possible to use highlighters as Mr. Prime suggests, but this
is what JTextPane was made for. It need not make your head spin.
For instance, here's a simple example that appends colored text.

disclaimer: I wrote that example. That site took it from my book.
It's from chapter 22, which is a hundred-page introduction to
JTextPane and AttributeSets. You can read part of it at
Google Books (click on "more �" under "Contents", then on
"Styled Text Panes") but some pages are missing. My publisher
would prefer you purchased the book.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!