Note: Advice in this article will only work for JxBrowser 6. See the corresponding article for JxBrowser 7 here.(注意:本文中的建议仅适用于JxBrowser6,JxBrowser7相应文章请点击这里。)


JxBrowser API provides functionality that allows on the currently loaded web page:(JxBrowser API提供的功能允许在当前加载的网页上:)

  • Finding specified text(查找特定文字)
  • Highlighting all matches(高亮所有匹配的文字)
  • Selecting first match(选中第一个匹配的文字)

To find specified text on the loaded web page use the Browser.findText() method. This method returns SearchResult instance that provides access to search results such as number of matches and index of selected match.(要在加载的网页上查找指定的文本,请使用Browser.findText()方法。此方法返回SearchResult实例,该实例提供对搜索结果的访问,例如匹配数和所选匹配项的索引。)


Note: Browser performs search only through visible content on the loaded document. If some text presented on the web page isn't visible due to CSS rules, Browser will not go through this content during search. Also, Browser doesn't search text on the document with size 0x0, so make sure that Browser component is visible and its size isn't empty.(注意:Browser只搜索加载的文档上的可见内容。如果因为CSS规则导致网页上的某些文本不可见,Browser将不会在搜索此内容。另外,Browser不会在文档中搜索大小为0x0的文本,因此请确保Browser组件可见并且其大小不为空。)


To clear highlights on a web page (search results) and cancel search use Browser.stopFindingText(StopFindAction action) method.(要清除网页上的高亮文本(搜索结果)并取消搜索,请使用Browser.stopFindingText(StopFindAction action)方法。)

import com.teamdev.jxbrowser.chromium.Browser;
import com.teamdev.jxbrowser.chromium.SearchParams;
import com.teamdev.jxbrowser.chromium.SearchResult;
import com.teamdev.jxbrowser.chromium.events.FinishLoadingEvent;
import com.teamdev.jxbrowser.chromium.events.LoadAdapter;
import com.teamdev.jxbrowser.chromium.swing.BrowserView;

import javax.swing.*;
import java.awt.*;

/**
 * This sample demonstrates, how to find text on the loaded web page.
 */
public class FindTextSample {
    public static void main(String[] args) {
        final Browser browser = new Browser();
        BrowserView browserView = new BrowserView(browser);

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.add(browserView, BorderLayout.CENTER);
        frame.setSize(700, 500);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

        browser.addLoadListener(new LoadAdapter() {
            @Override
            public void onFinishLoadingFrame(FinishLoadingEvent event) {
                if (event.isMainFrame()) {
                    SearchParams request = new SearchParams("find me");
                    // Find text from the beginning of the loaded web page.
                    SearchResult result = browser.findText(request);
                    System.out.println(result.indexOfSelectedMatch() + "/" +
                            result.getNumberOfMatches());
                    // Find the same text again from the currently selected match.
                    result = browser.findText(request);
                    System.out.println(result.indexOfSelectedMatch() + "/" +
                            result.getNumberOfMatches());
                }
            }
        });
        browser.loadHTML("<html><body><p>Find me</p><p>Find me</p></body></html>");
    }
}