aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes To change the color of a few characters in a JTextArea - urgent Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "To change the color of a few characters in a JTextArea - urgent" Watch "To change the color of a few characters in a JTextArea - urgent" New topic
Author

To change the color of a few characters in a JTextArea - urgent

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
How do I change the color of a few characters in a JTextArea. Does it support this feature?
My need, I want to type in a few words, as I type it I will write some validation such taht it changes color if it break the rule.
My question is which component supports this feature of changing selected portions of text to different color.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Check out the methods in JTextComponent, which JTextArea extends. Use setSelectionStart() and setSelectionEnd() to specify the text you want to change the color of, and then use setSelectionColor() or setSelectedTextColor() to change the color of the background or foreground. Note - I haven't actually done this myself, but that's what it looksl like from a quick browse of the API. Good luck.


"I'm not back." - Bill Harding, Twister
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Thanks everybody. I just got a reply for the same from a friend.
He says
Hi Lionel!
It is easiest to do this in a JTextPane. Create a new DefaultStyledDocument, and then create a JTextPane around this, and add the JTextPane to your frame/applet/whatever.
DefaultStyledDocument doc = new DefaultStyledDocument();
myFrame.getContentPane().add(new JTextPane(doc));
Next, you will need to make an AttributeSet, which, for instance, sets the foreground colour to red. The concept of a foreground colour setting is represented by the object StyleConstants.ColorConstants.Foreground, and to specify what this should be set to, use the java.awt.Color constants.
SimpleAttributeSet attribs = new SimpleAttributeSet();
attribs.addAttribute(StyleConstants.ColorConstants.Foreground,Color.red);
This would define an AttributeSet which would set the text colour to red. To apply this to part of the document, we could use (for instance):
doc.setCharacterAttributes(4,3,attribs,true);
(the parameters are offset into document, number of chars over which to apply the style, the AttributeSet, and whether to replace the existing style).
To use this for syntax highlighting, you will need to implement the DocumentListener interface, and respond to changes to the document by making appropriate highlights.
If you are thinking of writing a complex editor, you should consider having a look at JEditorPane.

As for your answer, it works.
So thanks, anyway. Hope this is a learning for both of us.
Frank Hale
Ranch Hand

Joined: Mar 25, 2000
Posts: 230
Have a look at the open source editor JEdit and also Jext. Search for both of them on sourceforge.net to get a URL. They are both excellent pure java editors which have very nice syntax highlighting. The highlighting engine was written by the author of JEdit and also used in Jext. This engine is very popular and used in other editors. Have a look at the code to see how its done. I am sure that will be a good reference for what your trying to do.
Have fun,
Frank
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hey thanks man, the editor is great fun indeed. Though there are only class files in the jar file I believe I will use jad to decompile and find its sourcer code.
Thanks once again. Feel free to disturb me any time if you want to ask me something.
Frank Hale
Ranch Hand

Joined: Mar 25, 2000
Posts: 230
You don't have to decompile them. There source code is open sourced. They probably have two downloads one for binary and the other for source code. You can decompile it if you choose but I would download the code from there site because there are nice comments in the code to describe what is going on.
Have fun,
Frank
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
If you dont mind can you just send me the same if you have it as the zip i downloaded form this site does not open properly as the file is corrupted while data is transferred. I have done it twice already.
Please help me.
Frank Hale
Ranch Hand

Joined: Mar 25, 2000
Posts: 230
Try downloading Jext:
http://www.chez.com/powerteam/jext/download.html
Try out Jipe also:
http://www.cheapnet.co.uk/~eis/jipe/
They both use the same syntax highlighting code as Jedit.
Frank
Bhagya Tangutur
Ranch Hand

Joined: Oct 21, 2002
Posts: 88
are these sites changed again?i am not able to open either of these.will be glad if some one tell me where to go to download the src code.
Thanks in advance


Sun Certified Java Programmer
Drake Silver
Ranch Hand

Joined: Apr 21, 2002
Posts: 80
Originally posted by Frank Hale:
Try downloading Jext:
http://www.chez.com/powerteam/jext/download.html
Try out Jipe also:
http://www.cheapnet.co.uk/~eis/jipe/
They both use the same syntax highlighting code as Jedit.
Frank

yeah, the links lead to 404..bad links.
you can find jext at:
www.jext.org
i think...


"Shh, Zog....here come one Now!"
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: To change the color of a few characters in a JTextArea - urgent
 
Similar Threads
scrollbar as needed: but in a TextArea, not a JTextArea
Setting colors
JTextArea color
How do you set the color of an array List of JTextArea
Problem in JFrame