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 you can use to handle loading and decide whether specified URL should be loaded in Chromium engine or not. The following example demonstrates how to register LoadHandler and cancel navigation to all URL that starts with http://www.google: (JxBrowser API提供了可用于处理加载并决定是否应在Chromium引擎中加载指定的URL的功能。以下示例演示了如何注册LoadHandler以及取消对以http://www.google开头的所有URL的导航:)

import com.teamdev.jxbrowser.chromium.Browser;
import com.teamdev.jxbrowser.chromium.DefaultLoadHandler;
import com.teamdev.jxbrowser.chromium.LoadParams;
import com.teamdev.jxbrowser.chromium.swing.BrowserView;

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

/**
 * This sample demonstrates how to cancel loading of a specific URL.
 */
public class LoadHandlerSample {
    public static void main(String[] args) {
        Browser browser = new Browser();
        BrowserView view = new BrowserView(browser);

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

        browser.setLoadHandler(new DefaultLoadHandler() {
            public boolean onLoad(LoadParams params) {
                // Cancel loading URL that starts with http://www.google
                return params.getURL().startsWith("http://www.google");
            }
        });
        browser.loadURL("http://www.google.com");
    }
} 

If you run the example above, you should see a white screen that indicates that the requested URL loading was suppressed because of our LoadHandler implementation.(如果运行上面的示例,应该会看到一个白屏,表明由于我们的LoadHandler实现,请求的URL加载被抑制。)