velsachin
velsachin

Reputation: 905

search and highlight text in webview in javafx

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?

Upvotes: 3

Views: 2429

Answers (1)

dankito
dankito

Reputation: 1108

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 */ }

Upvotes: 3

Related Questions