Samples

Browser Context

import com.teamdev.jexplorer.Browser;
import com.teamdev.jexplorer.BrowserContext;

/**
 * The sample demonstrates how to create two Browser instances that will run in one native process and
 * share session cookies and cache data. browserOne will access session cookies of browserTwo and vice versa.
 */
public class BrowserContextSample {
    public static void main(String[] args) {
        BrowserContext context = new BrowserContext();
        Browser browserOne = new Browser(context);
        Browser browserTwo = new Browser(context);
    }
}

Browser Features

import com.teamdev.jexplorer.Browser;
import com.teamdev.jexplorer.InternetFeature;

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

/**
 * The sample demonstrates how to configure extended Internet Explorer features.
 */
public class BrowserFeaturesSample {
    public static void main(String[] args) {
        Browser browser = new Browser();
        browser.setInternetFeatureEnabled(InternetFeature.DISABLE_NAVIGATION_SOUNDS, true);
        browser.setInternetFeatureEnabled(InternetFeature.RESTRICT_FILEDOWNLOAD, true);

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.add(browser, BorderLayout.CENTER);
        frame.setSize(800, 600);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

        browser.navigate("http://www.google.com");
    }
}

Browser Mode

import com.teamdev.jexplorer.Browser;
import com.teamdev.jexplorer.BrowserFeatures;
import com.teamdev.jexplorer.BrowserMode;

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

/**
 * The sample demonstrates how to configure MS IE 11 Browser Mode.
 */
public class BrowserModeSample {
    public static void main(String[] args) {
        // Enables MS IE 11 Browser Mode
        BrowserFeatures.enableBrowserMode(BrowserMode.IE11);
        Browser browser = new Browser();

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.add(browser, BorderLayout.CENTER);
        frame.setSize(800, 600);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

        browser.navigate("http://www.google.com");
    }
}

Load URL

import com.teamdev.jexplorer.Browser;

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

/**
 * The sample demonstrates how to create Browser instance, embed it, load and display some web page.
 */
public class BrowserSample {
    public static void main(String[] args) {
        Browser browser = new Browser();

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.add(browser, BorderLayout.CENTER);
        frame.setSize(800, 600);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

        browser.navigate("http://www.google.com");
    }
}

Load HTML

import com.teamdev.jexplorer.Browser;
import com.teamdev.jexplorer.event.NavigationAdapter;

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

/**
 * The sample demonstrates how to load HTML string.
 */
public class SetContentSample {
    public static void main(String[] args) {
        Browser browser = new Browser();

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.add(browser, BorderLayout.CENTER);
        frame.setSize(800, 600);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

        browser.addNavigationListener(new NavigationAdapter() {
            @Override
            public void mainDocumentCompleted(Browser browser, String url) {
                System.out.println("HTML is loaded.");
            }
        });
        browser.setContent("<html><body><h1>Load HTML Sample</h1></body></html>");
    }
}

Get HTML

import com.teamdev.jexplorer.Browser;
import com.teamdev.jexplorer.event.NavigationAdapter;

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

/**
 * The sample demonstrates how to get HTML content of the currently loaded document.
 */
public class GetContentSample {
    public static void main(String[] args) {
        Browser browser = new Browser();

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.add(browser, BorderLayout.CENTER);
        frame.setSize(800, 600);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

        browser.addNavigationListener(new NavigationAdapter() {
            @Override
            public void mainDocumentCompleted(Browser browser, String url) {
                System.out.println("html = " + browser.getContent());
            }
        });
        browser.navigate("http://www.google.com");
    }
}

Cookies

import com.teamdev.jexplorer.Browser;
import com.teamdev.jexplorer.Cookie;
import com.teamdev.jexplorer.event.NavigationAdapter;

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

/**
 * The sample demonstrates how to get all cookies for specified URL.
 */
public class CookieSample {
    public static void main(String[] args) {
        Browser browser = new Browser();

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.add(browser, BorderLayout.CENTER);
        frame.setSize(800, 600);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

        browser.addNavigationListener(new NavigationAdapter() {
            @Override
            public void mainDocumentCompleted(Browser browser, String url) {
                List<Cookie> cookies = browser.getCookies("http://www.google.com");
                for (Cookie cookie : cookies) {
                    System.out.println("cookie.getName() = " + cookie.getName());
                    System.out.println("cookie.getValue() = " + cookie.getValue());
                }
            }
        });
        browser.navigate("http://www.google.com");
    }
}

Dispose Browser

import com.teamdev.jexplorer.Browser;
import com.teamdev.jexplorer.event.DisposeListener;

/**
 * The sample demonstrates how to dispose Browser instance when you don't need to use it anymore.
 */
public class DisposeBrowserSample {
    public static void main(String[] args) {
        Browser browser = new Browser();
        browser.addDisposeListener(new DisposeListener() {
            @Override
            public void onDispose(Browser browser) {
                // Browser instance was disposed.
            }
        });
        browser.dispose();
    }
}

JavaScript

import com.teamdev.jexplorer.Browser;
import com.teamdev.jexplorer.JSValue;

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

/**
 * The sample demonstrates how to execute JavaScript code on the currently loaded web page.
 */
public class JavaScriptSample {
    public static void main(String[] args) {
        Browser browser = new Browser();

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.add(browser, BorderLayout.CENTER);
        frame.setSize(800, 600);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

        browser.executeScript("document.write('<html><title>My Title</title><body>Hello from JExplorer!</body></html>');");
        JSValue returnValue = browser.executeScript("document.title");
        if (returnValue.isString()) {
            System.out.println("Title = " + returnValue.getString());
        }
    }
}

JavaScript Errors

import com.teamdev.jexplorer.Browser;
import com.teamdev.jexplorer.ScriptErrorEvent;
import com.teamdev.jexplorer.ScriptErrorListener;

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

/**
 * The sample demonstrates how to listen to JavaScript errors.
 */
public class JavaScriptErrorListenerSample {
    public static void main(String[] args) {
        Browser browser = new Browser();

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.add(browser, BorderLayout.CENTER);
        frame.setSize(800, 600);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

        browser.addScriptErrorListener(new ScriptErrorListener() {
            @Override
            public void errorOccured(ScriptErrorEvent event) {
                System.out.println("event = " + event);
            }
        });

        browser.executeScript("a.unknown;");
    }
}

JavaScript Dialogs

import com.teamdev.jexplorer.Browser;
import com.teamdev.jexplorer.DialogHandler;

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

/**
 * The sample demonstrates how to handle JavaScript dialogs such as Alert or Confirmation.
 */
public class JavaScriptDialogsSample {
    public static void main(String[] args) {
        Browser browser = new Browser();

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.add(browser, BorderLayout.CENTER);
        frame.setSize(800, 600);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

        browser.setDialogHandler(new DialogHandler() {
            @Override
            public int showDialog(String title, String text, int type) {
                System.out.println("title = " + title);
                System.out.println("text = " + text);
                // Suppress dialog and close it right away without displaying UI
                return DialogHandler.IDOK;
            }
        });

        browser.executeScript("alert('test');");
    }
}

JavaScript Java Bridge

import com.teamdev.jexplorer.Browser;
import com.teamdev.jexplorer.BrowserFunction;
import com.teamdev.jexplorer.JSValue;

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

/**
 * The sample demonstrates how to register Java function callback to invoke Java
 * code from JavaScript.
 */
public class JavaScriptJavaSample {
    public static void main(String[] args) {
        Browser browser = new Browser();

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.add(browser, BorderLayout.CENTER);
        frame.setSize(800, 600);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

        browser.registerFunction("MyFunction", new BrowserFunction() {
            @Override
            public Object invoke(Object... args) {
                for (Object arg : args) {
                    System.out.println("arg = " + arg);
                }
                return "MyString";
            }
        });
        JSValue returnValue = browser.executeScript("MyFunction('abc', 1, true);");
        if (returnValue.isString()) {
            System.out.println("Function returns string: " + returnValue.getString());
        }
    }
}

Key Filter

import com.teamdev.jexplorer.Browser;
import com.teamdev.jexplorer.KeyFilter;
import com.teamdev.jexplorer.KeyFilterEvent;

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

/**
 * The sample demonstrates how to filter key events to disable Ctrl+A shortcut.
 */
public class KeyFilterSample {
    public static void main(String[] args) {
        Browser browser = new Browser();

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.add(browser, BorderLayout.CENTER);
        frame.setSize(800, 600);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

        browser.setKeyFilter(new KeyFilter() {
            @Override
            public boolean isFilter(KeyFilterEvent evt) {
                // Disable Ctrl+A
                return evt.isControlPressed() && evt.getKeyCode() == KeyEvent.VK_A;
            }
        });
        browser.navigate("http://www.teamdev.com");
    }
}

HTML to PNG

import com.teamdev.jexplorer.Browser;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.RenderedImage;
import java.io.File;
import java.io.IOException;

/**
 * The sample demonstrates how to get image of the currently loaded web page.
 */
public class HTMLToImageSample {
    public static void main(String[] args) throws IOException {
        final Browser browser = new Browser();

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.add(browser, BorderLayout.CENTER);
        frame.setSize(800, 600);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

        Browser.invokeAndWaitLoadMainFrame(browser, new Runnable() {
            @Override
            public void run() {
                browser.navigate("http://www.google.com");
            }
        });
        Image image = browser.getScreenShot(new Dimension(1280, 1024));
        ImageIO.write((RenderedImage) image, "PNG", new File("image.png"));
    }
}

Basic, NTLM, Digest Authentication

import com.teamdev.jexplorer.AuthenticationHandler;
import com.teamdev.jexplorer.Browser;

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

/**
 * The sample demonstrates how to handle Basic, NTLM and Digest authentication.
 */
public class AuthenticationSample {
    public static void main(String[] args) {
        final Browser browser = new Browser();
        browser.setAuthenticationHandler(new AuthenticationHandler() {
            @Override
            public void onAuthenticate() {
            }

            @Override
            public String getUserName() {
                return "user";
            }

            @Override
            public String getPassword() {
                return "passwd";
            }
        });

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.add(browser, BorderLayout.CENTER);
        frame.setSize(800, 600);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

        browser.navigate("http://httpbin.org/basic-auth/user/passwd");
    }
}

DOM Document

import com.teamdev.jexplorer.Browser;
import com.teamdev.jexplorer.dom.DOMDocument;
import com.teamdev.jexplorer.event.NavigationAdapter;

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

/**
 * The sample demonstrates how to access document of the loaded web page.
 */
public class DOMGetDocumentSample {
    public static void main(String[] args) {
        Browser browser = new Browser();

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.add(browser, BorderLayout.CENTER);
        frame.setSize(800, 600);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

        browser.addNavigationListener(new NavigationAdapter() {
            @Override
            public void mainDocumentCompleted(Browser browser, String url) {
                DOMDocument document = browser.getDocument();
                String title = document.getTitle();
                System.out.println("title = " + title);
            }
        });
        browser.navigate("http://www.google.com");
    }
}

DOM Elements

import com.teamdev.jexplorer.Browser;
import com.teamdev.jexplorer.dom.DOMDocument;
import com.teamdev.jexplorer.dom.DOMNodeList;
import com.teamdev.jexplorer.event.NavigationAdapter;

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

/**
 * The sample demonstrates how to get all elements with tag A and print their text content.
 */
public class DOMGetElementsSample {
    public static void main(String[] args) {
        Browser browser = new Browser();

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.add(browser, BorderLayout.CENTER);
        frame.setSize(800, 600);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

        browser.addNavigationListener(new NavigationAdapter() {
            @Override
            public void mainDocumentCompleted(Browser browser, String url) {
                DOMDocument document = browser.getDocument();
                DOMNodeList elements = document.getElementsByTagName("a");
                int length = elements.getLength();
                for (int i = 0; i < length; i++) {
                    System.out.println("Text Content: " + elements.item(i).getTextContent());
                }
            }
        });
        browser.navigate("http://www.google.com");
    }
}

Print

import com.teamdev.jexplorer.Browser;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * The sample demonstrates how to print currently loaded web page.
 */
public class PrintSample {
    public static void main(String[] args) {
        final Browser browser = new Browser();

        JButton printButton = new JButton("Print...");
        printButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                browser.print(true, "My Header", "My Footer");
            }
        });

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.add(printButton, BorderLayout.NORTH);
        frame.add(browser, BorderLayout.CENTER);
        frame.setSize(800, 600);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

        browser.navigate("http://www.google.com");
    }
}

Proxy

import com.teamdev.jexplorer.Browser;
import com.teamdev.jexplorer.ConnectionType;
import com.teamdev.jexplorer.ProxyConfiguration;
import com.teamdev.jexplorer.ServerType;

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

/**
 * The sample demonstrates how to set proxy settings for Browser instance.
 */
public class ProxySample {
    public static void main(String[] args) {
        ProxyConfiguration configuration = new ProxyConfiguration();
        configuration.setConnectionType(ConnectionType.PROXY);
        configuration.setProxy("127.0.0.1:8088", ServerType.HTTP);
        configuration.setProxy("127.0.0.1:8088", ServerType.SECURE);

        Browser browser = new Browser();
        browser.setProxy(configuration);

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.add(browser, BorderLayout.CENTER);
        frame.setSize(800, 600);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

        browser.navigate("http://www.google.com");
    }
}

Popups

import com.teamdev.jexplorer.Browser;
import com.teamdev.jexplorer.PopupContainer;
import com.teamdev.jexplorer.PopupHandler;
import com.teamdev.jexplorer.event.DisposeListener;
import com.teamdev.jexplorer.event.WindowAdapter;

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

/**
 * The sample demonstrates how to register your own popup handler to display popup windows
 * in the way you want.
 */
public class PopupSample {
    public static void main(String[] args) {
        Browser browser = new Browser();
        browser.setPopupHandler(new PopupHandler() {
            @Override
            public PopupContainer handlePopup() {
                return new PopupContainer() {
                    @Override
                    public void insertBrowser(final Browser browser) {
                        final JFrame frame = new JFrame();
                        frame.getContentPane().add(browser, BorderLayout.CENTER);
                        frame.setSize(800, 600);
                        frame.setLocationRelativeTo(null);
                        frame.setVisible(true);

                        frame.addWindowListener(new java.awt.event.WindowAdapter() {
                            @Override
                            public void windowClosing(WindowEvent e) {
                                browser.dispose();
                            }
                        });

                        browser.addDisposeListener(new DisposeListener() {
                            @Override
                            public void onDispose(Browser browser) {
                                SwingUtilities.invokeLater(new Runnable() {
                                    @Override
                                    public void run() {
                                        frame.setVisible(false);
                                        frame.dispose();
                                    }
                                });
                            }
                        });

                        browser.addWindowListener(new WindowAdapter() {
                            @Override
                            public void onSetLeft(int left) {
                                Rectangle bounds = frame.getBounds();
                                bounds.x = left;
                                frame.setBounds(bounds);
                            }

                            @Override
                            public void onSetTop(int top) {
                                Rectangle bounds = frame.getBounds();
                                bounds.y = top;
                                frame.setBounds(bounds);
                            }

                            @Override
                            public void onSetWidth(int width) {
                                Rectangle bounds = frame.getBounds();
                                bounds.width = width;
                                frame.setBounds(bounds);
                            }

                            @Override
                            public void onSetHeight(int height) {
                                Rectangle bounds = frame.getBounds();
                                bounds.height = height;
                                frame.setBounds(bounds);
                            }

                            @Override
                            public void onResizable(boolean resizable) {
                                frame.setResizable(resizable);
                            }
                        });
                    }
                };
            }
        });

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.add(browser, BorderLayout.CENTER);
        frame.setSize(800, 600);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

        browser.navigate("http://www.popuptest.com");
    }
}

User-Agent

import com.teamdev.jexplorer.Browser;

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

/**
 * The sample demonstrates how to override default user-agent string.
 */
public class UserAgentSample {
    public static void main(String[] args) {
        Browser browser = new Browser();
        browser.setUserAgent("MyUserAgent");

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.add(browser, BorderLayout.CENTER);
        frame.setSize(800, 600);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

        browser.navigate("http://whatsmyuseragent.com/");
    }
}