I have load the local html file in the web engine. i need to search and highlight the given string in the web view page.
Is there any way to do that?
Asked
Active
Viewed 2,276 times
3

velsachin
- 905
- 2
- 8
- 13
-
Hi, did you find out how to do it? I''m stuck at it as well... – SharonBL Oct 17 '13 at 04:59
-
Check [this](http://stackoverflow.com/questions/11790375/how-to-highlight-all-the-occurrence-of-a-particular-string-in-a-div-with-java-sc) out :) – Muten Roshi Oct 23 '14 at 21:38
-
You need to write Javascript code, wich you can call over java with the Nashorn Api. – Marcel Jun 11 '15 at 11:48
-
Possible duplicate of [JavaFx | Search and Highlight text | Add Search Bar for loaded web page](http://stackoverflow.com/questions/19418626/javafx-search-and-highlight-text-add-search-bar-for-loaded-web-page) – Michael Mrozek Apr 20 '16 at 16:28
1 Answers
2
If you don't mind using reflection, it can be done natively in Java code.
WebEngine has a private field page of type WebPage, which in turn has this method which exactly does what you want:
// Find in page
public boolean find(String stringToFind, boolean forward, boolean wrap, boolean matchCase) {
// ...
}
So to access this find() method you have to do:
WebView webView = new WebView();
WebEngine engine = webView.getEngine();
try {
Field pageField = engine.getClass().getDeclaredField("page");
pageField.setAccessible(true);
WebPage page = (com.sun.webkit.WebPage) pageField.get(engine);
page.find("query", true, true, false);
} catch(Exception e) { /* log error could not access page */ }

dankito
- 958
- 7
- 16