Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to color different colors of the string in a textpane

 
prajna pa
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i had used String builder for appending the data and i am converting to string in textpane . so i need different words in different color so how should i do this using This String Builder.
Please help me... Thanks in advance.

 
Darryl Burke
Bartender
Posts: 5148
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By 'textpane' do you mean JTextPane?
 
Rob Spoor
Sheriff
Pie
Posts: 20608
63
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
StringBuilder doesn't know anything about colours. StyledDocument does, and that's what's being used by JTextPane. Just check out the methods that take an AttributeSet.
 
prajna pa
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes i mean JTextpane
 
prajna pa
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
my program is to append the nos converted to string each time from the for loop so for appending i am using string builder and then converting to string and setting to JTextPane.

my problem is when i am searching one no in the entered JTextPane. that no should appear in color..i am getting that no.. but i am not knowing how to color it ... please help me sir...i need to submit this assignment as soon as possible
 
Rob Spoor
Sheriff
Pie
Posts: 20608
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out javax.swing.text.Highlighter.
 
prajna pa
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



here in my GUI by clicking on the search button flag becomes true and it checks the if condition in that "currentValueLat" will come from for loop and "getLatSearch()" will be from the user (search value)

and here i need to color that value and sb is the string builder object . appendcolor is a function and i will the code for that



so how to do this i am a beginner for Java... so please help me ... thank you all in advance
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic