954,536 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

String Colors

Hi,

I want to create a typewrite/typing java app. I want to change the color of a text to be different, whenever the user types the wrong character in a JTextArea. For example, if the user was supposed to type "My Book" and s/he types "My Bok", I want the letter "k" to be displayed in red.

Any helpful input is greatly appreciated.

Thanks.

ChangBroot
Newbie Poster
5 posts since Aug 2009
Reputation Points: 10
Solved Threads: 0
 

Good luck, because this is not something you cannot accomplish in one-two weeks. Big teams of programmers spent months on setting this functionality for products like Microsoft Office or Open Office.

peter_budo
Code tags enforcer
Moderator
15,436 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 902
 

And why can't he override paintComponent, setting the color to whatever he wants, using the drawString method to write text, then changing the color when he wants to?

BestJewSinceJC
Posting Maven
2,772 posts since Sep 2008
Reputation Points: 874
Solved Threads: 354
 

Most Java text components support HTML, so simply use HTML tags to control fonts, colors etc
http://java.sun.com/docs/books/tutorial/uiswing/components/html.html

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

I had seen that also, James, but it said JEditorPane and JTextPane among others, whereas the OP wants to use JTextPane. I guess it might be supported for JTextPane as well though.

BestJewSinceJC
Posting Maven
2,772 posts since Sep 2008
Reputation Points: 874
Solved Threads: 354
 

paintComponent doesn't solve that problem, because it paints. I need something to be reused. For example, if the user wants to delete a character, it should be deleted.

ChangBroot
Newbie Poster
5 posts since Aug 2009
Reputation Points: 10
Solved Threads: 0
 
Most Java text components support HTML, so simply use HTML tags to control fonts, colors etc http://java.sun.com/docs/books/tutorial/uiswing/components/html.html

Thanks, I'll have a look at it.

ChangBroot
Newbie Poster
5 posts since Aug 2009
Reputation Points: 10
Solved Threads: 0
 
I had seen that also, James, but it said JEditorPane and JTextPane among others, whereas the OP wants to use JTextPane. I guess it might be supported for JTextPane as well though.

You're right, I think I should go with JTextPane. This is what I was told in forum.java.sun.com. Thanks Jew.

ChangBroot
Newbie Poster
5 posts since Aug 2009
Reputation Points: 10
Solved Threads: 0
 

Did you get anywhere with this ChangBroot? I'm trying to do a similar thing, but by placing a smiley in place of some text.

Here's my thread:
http://www.daniweb.com/forums/thread210843.html

TheWhite
Junior Poster
174 posts since May 2008
Reputation Points: 72
Solved Threads: 6
 

Did you get anywhere with this ChangBroot? I'm trying to do a similar thing, but by placing a smiley in place of some text.

Here's my thread: http://www.daniweb.com/forums/thread210843.html

Yes, actually, I found out that JTextArea is only for plane AND unformattable text. So, I had to use JTextPane, because it is a child of JEditorPane and add more functionality to JEditorPane. Therefore, for your program, you should use JEditorPane OR JTextPane, cause they give you tons of abilities to manipulate texts, images or icons. Good luck my friend.

ChangBroot
Newbie Poster
5 posts since Aug 2009
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You