This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Highlighting certain strings

 
S. Jones
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, so I have text in my JTextArea. A method is called to break up that text into sentences stored in an ArrayList. It is necessary for this to happen so I can find certain strings within the sentence.
So right now i have the original in the JTextArea and a copy broken up into sentences in an ArrayList.

My Goal is to highlight the string I found (in the ArrayList through other methods), but only in the place where i found it in the sentence.
ex.) "The yellow pineapple is yellow." Lets say the string I found (in the ArrayList) was the last "yellow" in the sentence.

My Problem is How can i find that exact same "yellow" in the original text and highlight it?

This has really been messing me up so any insight would be really helpful and appreciated. Thank you.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49442
62
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You cannot simply do that in a String, because a String does not have any notion of location. Nor is there any distinction in the String class between "yellow" and "yellow". Even if you have references to different objects both "yellow", the equals() method will not find any discintion between the two.

Back to the old technology, pencil and paper. Write how you are going to do that on paper first. Eraser, too, because you will make mistakes.

You can try writing a class with highlighting, where as well as a String, you have markers for “on” and “off”, possibly stored in a List.

I think this question too difficult for “beginning”, so I shall move it.
 
Darryl Burke
Bartender
Posts: 5132
11
Java Netbeans IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jones, please BeForthrightWhenCrossPostingToOtherSites. You should have at the very least told us here that the question had already been answered elsewhere.
http://www.java-forums.org/new-java/59121-highlighting-certain-strings.html
 
S. Jones
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Darryl Burke wrote:Jones, please BeForthrightWhenCrossPostingToOtherSites. You should have at the very least told us here that the question had already been answered elsewhere.
http://www.java-forums.org/new-java/59121-highlighting-certain-strings.html

I apologize. I wasn't really aware of that rule. I'll make sure to follow it now.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic