• 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

Text editor question...

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
I need to develop a text editor with the following two
features:
1. Quoted text should appear in a colour different from
that of normal text.
2. Selected words should be recognized by the editor as
key words and should appear in a colour different from
that of normal or quoted text.
This is new territory for me. I know that I should use a
JTextPane, but after that I'm not sure. I was thinking of
implement a CaretListener and/or a DocumentListener to implement
the aforementioned features. Am I on the right track? Would
implementing a custom EditorKit be a better strategy?
Thanks,
Dennis
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mr.Dennis,
Yes u can achieve this with TextPane. U have to get the textpanes StyledDocument and use SimpleAttributeSet class
to achieve this functionality. I have furnished few lines of
code which will act as a guideline to solve ur problem.

JTextPane textpane=new JTextPane();
StyledDocument doc=textpane.getStyledDocument();
SimpleAttributeSet set=new SimpleAttributeSet();
javax.swing.text.StyleConstants.setForeground(set,new Color(0,35,3));
int startposition=5;
int lengthtobemarkedwith=6;
doc.setCharacterAttributes(startposition,lengthtobemarkedwith,set,false);

In this way u can bring about a different text color for a specied range of text in a textpane. Hope this would be useful
to u. All the best.
Regards,
Premila

Originally posted by Dennis Spathis:
Hi all,
I need to develop a text editor with the following two
features:
1. Quoted text should appear in a colour different from
that of normal text.
2. Selected words should be recognized by the editor as
key words and should appear in a colour different from
that of normal or quoted text.
This is new territory for me. I know that I should use a
JTextPane, but after that I'm not sure. I was thinking of
implement a CaretListener and/or a DocumentListener to implement
the aforementioned features. Am I on the right track? Would
implementing a custom EditorKit be a better strategy?
Thanks,
Dennis


 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic