• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 230
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 230
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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...
reply
    Bookmark Topic Watch Topic
  • New Topic