| Author |
Highlight the search field in a text area -----HELP required URGENT
|
Mayank Jaswal
Greenhorn
Joined: Mar 05, 2009
Posts: 22
|
|
|
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
Joined: Oct 27, 2005
Posts: 19216
|
|
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.
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
Mayank Jaswal
Greenhorn
Joined: Mar 05, 2009
Posts: 22
|
|
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
Joined: Oct 27, 2005
Posts: 19216
|
|
The usual way to search a string over and over:
Or, shorter:
|
 |
Mayank Jaswal
Greenhorn
Joined: Mar 05, 2009
Posts: 22
|
|
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
Joined: Oct 27, 2005
Posts: 19216
|
|
java.lang.String#indexOf(java.lang.String)
java.lang.String#indexOf(java.lang.String, int)
|
 |
Mayank Jaswal
Greenhorn
Joined: Mar 05, 2009
Posts: 22
|
|
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
Joined: Oct 27, 2005
Posts: 19216
|
|
|
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
Joined: Jun 13, 2009
Posts: 1788
|
|
|
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
Joined: Oct 27, 2005
Posts: 19216
|
|
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.
|
 |
 |
|
|
subject: Highlight the search field in a text area -----HELP required URGENT
|
|
|