Documentation‎ > ‎

Migrating from 3.x

This document describes how to upgrade your application from JxBrowser 3.x to 4.x API.

Distribution Package changes

Since JxBrowser 4 integrates only with Chromium engine, the number or required libraries was decreased. Now you need to include only the following JAR files into your application class path:

java -classpath jxbrowser-4.7.jar;jxbrowser-win-4.7.jar;jxbrowser-mac-4.7.jar;jxbrowser-linux32-4.7.jar;jxbrowser-linux64-4.7.jar <your-application-class>

Required libraries

 3.x libraries
jxbrowser-3.4.jar
xulrunner-windows.jar
xulrunner15-windows.jar
xulrunner15-mac.jar
xulrunner-mac.jar
engine-ie.jar
engine-webkit.jar
engine-gecko15.jar
engine-gecko.jar
slf4j-api-1.5.8.jar
slf4j-log4j12-1.5.8.jar
log4j-1.2.15.jar
winpack-3.8.4.jar
comfyj-2.10.jar
jniwrap-3.8.4.jar
 4.x libraries
jxbrowser-4.7.jar
jxbrowser-win-4.7.jar
jxbrowser-mac-4.7.jar
jxbrowser-linux32-4.7.jar
jxbrowser-linux64-4.7.jar

API changes

JxBrowser 3.x  JxBrowser 4.x
Creating Browser instance:
import com.teamdev.jxbrowser.Browser;
import com.teamdev.jxbrowser.BrowserFactory;
 
Browser browser = BrowserFactory.createBrowser();

import com.teamdev.jxbrowser.chromium.Browser;
import com.teamdev.jxbrowser.chromium.BrowserFactory;
 
Browser browser = BrowserFactory.create();
 
Accessing Browser component:
 
JComponent component = browser.getComponent();
JComponent component = browser.getView().getComponent();

Load URL:
 
browser.navigate(String url);

browser.loadURL(String url);
browser.loadURL(int frameId, String url);
Load URL with POST Data: 
browser.navigate(String url, String postData);
browser.loadURL(new LoadURLParams(String url, String postData));

Execute JavaScript:
 
String result = browser.executeScript(String javaScript);
browser.executeJavaScript(String javaScript);
browser.executeJavaScript(long frameId, String javaScript);
JSValue result = browser.executeJavaScriptAndReturnValue(String javaScript);
JSValue result = browser.executeJavaScriptAndReturnValue(long frameId, String javaScript);
JavaScript-Java Bridge: 
browser.registerFunction(String functionName, new BrowserFunction() {
    public Object invoke(Object... args) {
        return null;
    }
});
browser.registerFunction(String functionName, new BrowserFunction() {
    public JSValue invoke(JSValue... args) {
        return JSValue.createNull();
    }
});

Get URL:
 
String url = browser.getCurrentLocation();
String url = browser.getURL();
 
Load HTML:
 
browser.setContent(String html);
browser.setContent(String html, String baseURL);
browser.loadHTML(String html);
browser.loadHTML(int frameId, String html);
browser.loadHTML(LoadHTMLParams params);
browser.loadHTML(int frameId, LoadHTMLParams params);
browser.loadData(LoadDataParams params);
browser.loadData(int frameId, LoadDataParams params);
Get HTML: 
String html = browser.getContent();
String html = browser.getHTML();
String html = browser.getHTML(int frameId);
Get Document: 
org.w3c.dom.Document document = browser.getDocument();
DOMDocument document = browser.getDocument();
 
Get Cookie Storage:
 
HttpCookieStorage cookieStorage = browser.getCookieStorage();
List<HttpCookie> cookies = cookieStorage.getCookies();
CookieStorage cookieStorage = browser.getCookieStorage();
List<Cookie> allCookies = cookieStorage.getAllCookies();

Is loading:

boolean isLoading = !browser.isNavigationFinished();
boolean isLoading = browser.isLoading();
 
Wait ready (wait load completion):
 
browser.waitReady();
while (browser.isLoading()) {
    TimeUnit.MILLISECONDS.sleep(25);
}
Reload: 
browser.refresh();
browser.reload();
browser.reloadIgnoringCache();
browser.reload(boolean checkForRepost);
browser.reloadIgnoringCache(boolean checkForRepost);
Start frame loading:
 
browser.addNavigationListener(new NavigationAdapter() {
    @Override
    public void navigationStarted(NavigationEvent event) {
        // frame loading is started
    }
});
browser.addLoadListener(new LoadAdapter() {
    @Override
    public void onStartLoadingFrame(StartLoadingEvent event) {
        // frame loading is started
    }
});
 
Handle URL load:
 
browser.getServices().setWebPolicyDelegate(new DefaultWebPolicyDelegate() {
    @Override
    public boolean allowNavigation(NavigationEvent event) {
        return event.getUrl().contains("google.com");
    }
});
browser.setLoadHandler(new LoadHandler() {
    public boolean onLoad(LoadParams params) {
        return params.getURL().contains("google.com");
    }
});
 
Finish frame loading:
 
browser.addNavigationListener(new NavigationAdapter() {
    @Override
    public void navigationFinished(NavigationFinishedEvent event) {
        // frame loading is finished
    }
});
browser.addLoadListener(new LoadAdapter() {
    @Override
    public void onFinishLoadingFrame(FinishLoadingEvent event) {
        // frame loading is finished
    }
});
 
Fail frame loading:
 
browser.addNavigationListener(new NavigationAdapter() {
    @Override
    public void navigationFinished(NavigationFinishedEvent event) {
        if (event.isNavigationFailed()) {
            // frame loading is failed
        }
    }
});
browser.addLoadListener(new LoadAdapter() {
    @Override
    public void onFailLoadingFrame(FailLoadingEvent event) {
        // frame loading is failed
    }
});
 
Popup handling:
 
browser.getServices().setNewWindowManager(new NewWindowManager() {
    public NewWindowContainer evaluateWindow(NewWindowParams params) {
        return new NewWindowContainer() {
            public void insertBrowser(Browser browser) {
            }
        };
    }
});
browser.setPopupHandler(new PopupHandler() {
    public PopupContainer handlePopup(PopupParams params) {
        return new PopupContainer() {
            public void insertBrowser(Browser browser, 
                                      Rectangle initialBounds) {
            }
        };
    }
});
 
Context Menu Handler:
 
browser.setContextMenuHandler(new ContextMenuHandler() {
    public void showContextMenu(ContextMenuEvent event) {
        Point location = event.getLocation();
        Node target = event.getTarget();
    }
});
browser.setContextMenuHandler(new ContextMenuHandler() {
    public void showContextMenu(ContextMenuParams params) {
        Point location = params.getLocation();
        long frameId = params.getFrameId();
        String frameCharset = params.getFrameCharset();
        String frameURL = params.getFrameURL();
        String linkText = params.getLinkText();
        String linkURL = params.getLinkURL();
        MediaType mediaType = params.getMediaType();
        String pageURL = params.getPageURL();
        String selectionText = params.getSelectionText();
        String srcURL = params.getSrcURL();
    }
});
Set Zoom: 
browser.getConfigurable().setZoomValue(int zoomValue);
browser.getConfigurable().getZoomValue();
browser.setZoomLevel(double zoomLevel);
browser.getZoomLevel();
browser.zoomIn();
browser.zoomOut();
browser.zoomReset();

browser.addZoomListener(new ZoomListener() {
    public void onZoomChanged(ZoomEvent event) {
        String url = event.getURL();
        double zoomLevel = event.getZoomLevel();
    }
});
Dialog Handler: 
BrowserServices browserServices = BrowserServices.getInstance();
browserServices.setPromptService(new PromptService() {
    public void alertRequested(DialogParams params) {
    }
    ...
});
browser.setDialogHandler(new DefaultDialogHandler() {
    public void onAlert(DialogParams params) {
    }
    ...
});

Proxy Settings:
 
ProxyConfig proxyConfig = BrowserServices.getInstance().getProxyConfig();
proxyConfig.setProxy(ServerType.HTTP, new ProxyServer("127.0.0.1", 8080));
proxyConfig.setProxy(ServerType.SSL, new ProxyServer("127.0.0.1", 8080));
proxyConfig.setProxy(ServerType.FTP, new ProxyServer("127.0.0.1", 8080));
HostPortPair httpServer = new HostPortPair("127.0.0.1", 80);
HostPortPair httpsServer = new HostPortPair("127.0.0.1", 80);
HostPortPair ftpServer = new HostPortPair("127.0.0.1", 80);
Browser browser = BrowserFactory.create(new CustomProxyConfig(httpServer,
httpsServer, ftpServer));
 Load Handler: 
browser.getServices().setWebPolicyDelegate(new DefaultWebPolicyDelegate() {
    @Override
    public boolean allowNavigation(NavigationEvent event) {
        // return false to cancel navigation
        return event.getUrl().startsWith("http://www.google");
    }
});
browser.setLoadHandler(new LoadHandler() {
    public boolean onLoad(LoadParams params) {
        return !params.getURL().startsWith("http://www.google");
    }
});