• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Highlight the search field in a text area -----HELP required URGENT

 
Mayank Jaswal
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am searching for a field in the text area on the clicking of a button and wants to highlight it .When i am searching for the field i am only able to highlight the first existence of the field but i want to highlight all the existence of that field in text area at one go . Please help
 
Rob Spoor
Sheriff
Pie
Posts: 20610
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
http://faq.javaranch.com/java/EaseUp

You can find the first occurrance, then just search again from that point on for the next one. String has indexOf methods that take a starting index.
 
Mayank Jaswal
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:http://faq.javaranch.com/java/EaseUp

You can find the first occurrance, then just search again from that point on for the next one. String has indexOf methods that take a starting index.



that i tried but the problem here is that i am not able to start the search from the index where i first found that word. It is starting from the start of the text area .I tried it by creating some global variable where i can retain that but it is still not happening .
 
Rob Spoor
Sheriff
Pie
Posts: 20610
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The usual way to search a string over and over:
Or, shorter:
 
Mayank Jaswal
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:The usual way to search a string over and over:
Or, shorter:



Thanks for your reply Rob but the thing here is that i am noot able to understand how the code you had written will work it can only provide me with the first occurance of the string .But the outher ocurrance of the same string i am not able to get
 
Rob Spoor
Sheriff
Pie
Posts: 20610
63
Chrome Eclipse IDE Java Windows
 
Mayank Jaswal
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



I think problem is not clear .

I am able to find the index of the string for the first time

i.e If the string is "this is the tutorial "

and i want to highlight all the" t" letters .

Then by using the indexof("t"),
i am getting the index of first "t" only which is " o" but i also want to highlight t at other positions i.e 6,9,11 which i am not able to get .
 
Rob Spoor
Sheriff
Pie
Posts: 20610
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I already gave you the solution - use the indexOf method that takes a start position. I even provided the code for it.
 
Rob Camick
Ranch Hand
Posts: 2613
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to use a "Highlighter", you can't just "select" the text as you can only have one selection at a time.
 
Rob Spoor
Sheriff
Pie
Posts: 20610
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Correct. I (falsely) assumed that Mayank already was using a Highlighter.

To highlight, you need a DefaultHighLighter, then use its addHighlight method with a DefaultHighlighter.DefaultHighlightPainter. You can of course use other implementations of both Highlighter and Highlighter.HighlightPainter but these two implementations should already work for you.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic