wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes JTextArea - changing color of some words Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTextArea - changing color of some words" Watch "JTextArea - changing color of some words" New topic
Author

JTextArea - changing color of some words

Jeronimo Backes
Greenhorn

Joined: Sep 20, 2004
Posts: 29
It is possible to change the color and text formatting of some words of a text inside a JTextArea?

I made a little editor with that component, and that's the only thing I couldn't do yet.

I tried to change that class to inherit from JEditor pane, where I can colorize wathever I want with HTML. BUT, I wasn't able to get the funcionality that JTextArea provided: TabSize and RowHeaders that can show the line number (I wasn't able to retrieve the correct line height using html text... and I even had some trouble concerning caret positioning).

I tried to make a hack with the Highlighter class, but even changing the original source code from sun, I wasn't able to make some pieces of text appear with another color. In fact, they appear with the correct color, but behind the text.

Okay, I tried everything I could think of. Can someone give me a hand with that issue? That little thing caused enough pain.


The art of being crazy is to NEVER commit the crazyness of being normal.
Mark Van Tuyl
Ranch Hand

Joined: Mar 22, 2002
Posts: 60
The only way I'm aware of that you could do that would be to draw the text in a Graphics area.


<a href="http://www.catb.org/~esr/faqs/smart-questions.html" target="_blank" rel="nofollow">How To Ask Smart Questions</a>
Jeronimo Backes
Greenhorn

Joined: Sep 20, 2004
Posts: 29
Yeah, here's the next thing I'll try to do: override the paint(Graphics g) of JTextArea. It will surely work.

Thanks man.
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
Originally posted by Jer´┐Żnimo Backes:
It is possible to change the color and text formatting of some words of a text inside a JTextArea?


Is there a reason you need to use JTextArea instead of JTextPane?


This code is an excerpt from this code. If you want to create your own editor with JTextPane, I recommend you take a look at chapter 22 of O'Reilly's Java Swing, 2nd ed.
(Disclaimer: I am the author.)

JTextArea ignores anything you do with setCharacterAttributes(), so
this approach only works with JTextPane.


bitguru blog
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JTextArea - changing color of some words
 
Similar Threads
Beginning GUI programming exercise. Please help to figure out why it does not compile.
JComponent text in some JApplets don't show on Mac, do show on Windows PC
have a problem rendering a text in JOptionpane + JTextArea
Help about JTextArea in applet!
To change the color of a few characters in a JTextArea - urgent