Search in sources :

Example 26 with Browser

use of com.teamdev.jxbrowser.browser.Browser in project JxBrowser-Examples by TeamDev-IP.

the class JsFunctionCallback method main.

public static void main(String[] args) {
    try (Engine engine = Engine.newInstance(OFF_SCREEN)) {
        Browser browser = engine.newBrowser();
        browser.mainFrame().ifPresent(frame -> {
            JsObject jsObject = frame.executeJavaScript("window");
            if (jsObject != null) {
                // Inject JsFunctionCallback into JavaScript and associate it
                // with the "window.sayHelloTo" JavaScript property.
                jsObject.putProperty("sayHelloTo", (com.teamdev.jxbrowser.js.JsFunctionCallback) arguments -> "Hello, " + arguments[0]);
            }
            String greetings = frame.executeJavaScript("window.sayHelloTo('John')");
            System.out.println(greetings);
        });
    }
}
Also used : OFF_SCREEN(com.teamdev.jxbrowser.engine.RenderingMode.OFF_SCREEN) Browser(com.teamdev.jxbrowser.browser.Browser) JsObject(com.teamdev.jxbrowser.js.JsObject) Engine(com.teamdev.jxbrowser.engine.Engine) JsObject(com.teamdev.jxbrowser.js.JsObject) Engine(com.teamdev.jxbrowser.engine.Engine) Browser(com.teamdev.jxbrowser.browser.Browser)

Example 27 with Browser

use of com.teamdev.jxbrowser.browser.Browser in project JxBrowser-Examples by TeamDev-IP.

the class LoadHtml method main.

public static void main(String[] args) {
    Engine engine = Engine.newInstance(HARDWARE_ACCELERATED);
    Browser browser = engine.newBrowser();
    SwingUtilities.invokeLater(() -> {
        BrowserView view = BrowserView.newInstance(browser);
        JFrame frame = new JFrame("Load HTML");
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.add(view, BorderLayout.CENTER);
        frame.setSize(700, 500);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    });
    browser.mainFrame().ifPresent(mainFrame -> {
        mainFrame.loadHtml("<html><body><h1>Hello there!</h1></body></html>");
    });
}
Also used : JFrame(javax.swing.JFrame) BrowserView(com.teamdev.jxbrowser.view.swing.BrowserView) Engine(com.teamdev.jxbrowser.engine.Engine) Browser(com.teamdev.jxbrowser.browser.Browser)

Example 28 with Browser

use of com.teamdev.jxbrowser.browser.Browser in project JxBrowser-Examples by TeamDev-IP.

the class LoadHtmlThroughInterceptRequest method main.

public static void main(String[] args) {
    InterceptUrlRequestCallback interceptCallback = params -> {
        if (params.urlRequest().url().endsWith("?load-html")) {
            byte[] bytes = "<html><body>Hello!</body></html>".getBytes();
            UrlRequestJob job = params.newUrlRequestJob(UrlRequestJob.Options.newBuilder(HttpStatus.OK).addHttpHeader(HttpHeader.of("Content-Type", "text/html")).build());
            job.write(bytes);
            job.complete();
            return InterceptUrlRequestCallback.Response.intercept(job);
        }
        return InterceptUrlRequestCallback.Response.proceed();
    };
    Engine engine = Engine.newInstance(EngineOptions.newBuilder(HARDWARE_ACCELERATED).addScheme(Scheme.HTTP, interceptCallback).build());
    Browser browser = engine.newBrowser();
    SwingUtilities.invokeLater(() -> {
        BrowserView view = BrowserView.newInstance(browser);
        JFrame frame = new JFrame("Load Local Files");
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.add(view, BorderLayout.CENTER);
        frame.setSize(700, 500);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    });
    browser.navigation().loadUrl("http://localhost?load-html");
}
Also used : InterceptUrlRequestCallback(com.teamdev.jxbrowser.net.callback.InterceptUrlRequestCallback) UrlRequestJob(com.teamdev.jxbrowser.net.UrlRequestJob) HttpHeader(com.teamdev.jxbrowser.net.HttpHeader) BrowserView(com.teamdev.jxbrowser.view.swing.BrowserView) EngineOptions(com.teamdev.jxbrowser.engine.EngineOptions) HttpStatus(com.teamdev.jxbrowser.net.HttpStatus) Scheme(com.teamdev.jxbrowser.net.Scheme) SwingUtilities(javax.swing.SwingUtilities) Browser(com.teamdev.jxbrowser.browser.Browser) HARDWARE_ACCELERATED(com.teamdev.jxbrowser.engine.RenderingMode.HARDWARE_ACCELERATED) BorderLayout(java.awt.BorderLayout) JFrame(javax.swing.JFrame) Engine(com.teamdev.jxbrowser.engine.Engine) WindowConstants(javax.swing.WindowConstants) InterceptUrlRequestCallback(com.teamdev.jxbrowser.net.callback.InterceptUrlRequestCallback) JFrame(javax.swing.JFrame) BrowserView(com.teamdev.jxbrowser.view.swing.BrowserView) UrlRequestJob(com.teamdev.jxbrowser.net.UrlRequestJob) Engine(com.teamdev.jxbrowser.engine.Engine) Browser(com.teamdev.jxbrowser.browser.Browser)

Example 29 with Browser

use of com.teamdev.jxbrowser.browser.Browser in project JxBrowser-Examples by TeamDev-IP.

the class CustomProtocol method main.

public static void main(String[] args) {
    Engine engine = Engine.newInstance(EngineOptions.newBuilder(HARDWARE_ACCELERATED).addScheme(Scheme.of(PROTOCOL), new InterceptCustomSchemeCallback()).build());
    Browser browser = engine.newBrowser();
    invokeLater(() -> {
        BrowserView view = BrowserView.newInstance(browser);
        JFrame frame = new JFrame("Custom Protocol Handler");
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.add(view, BorderLayout.CENTER);
        frame.setSize(700, 500);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    });
    browser.navigation().loadUrl(PROTOCOL + "://hello");
}
Also used : JFrame(javax.swing.JFrame) BrowserView(com.teamdev.jxbrowser.view.swing.BrowserView) Engine(com.teamdev.jxbrowser.engine.Engine) Browser(com.teamdev.jxbrowser.browser.Browser)

Example 30 with Browser

use of com.teamdev.jxbrowser.browser.Browser in project JxBrowser-Examples by TeamDev-IP.

the class DefaultZoomLevel method main.

public static void main(String[] args) {
    Engine engine = Engine.newInstance(HARDWARE_ACCELERATED);
    Browser browser = engine.newBrowser();
    SwingUtilities.invokeLater(() -> {
        BrowserView view = BrowserView.newInstance(browser);
        JFrame frame = new JFrame("Change Default Zoom Level");
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.add(view, BorderLayout.CENTER);
        frame.setSize(700, 500);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    });
    engine.zoomLevels().defaultLevel(ZoomLevel.P_300);
    browser.navigation().loadUrl("https://www.google.com");
}
Also used : JFrame(javax.swing.JFrame) BrowserView(com.teamdev.jxbrowser.view.swing.BrowserView) Engine(com.teamdev.jxbrowser.engine.Engine) Browser(com.teamdev.jxbrowser.browser.Browser)

Aggregations

Browser (com.teamdev.jxbrowser.browser.Browser)73 Engine (com.teamdev.jxbrowser.engine.Engine)70 JFrame (javax.swing.JFrame)51 BrowserView (com.teamdev.jxbrowser.view.swing.BrowserView)47 BorderLayout (java.awt.BorderLayout)8 HARDWARE_ACCELERATED (com.teamdev.jxbrowser.engine.RenderingMode.HARDWARE_ACCELERATED)7 SwingUtilities (javax.swing.SwingUtilities)7 WindowConstants (javax.swing.WindowConstants)7 EngineOptions (com.teamdev.jxbrowser.engine.EngineOptions)6 Document (com.teamdev.jxbrowser.dom.Document)5 Frame (com.teamdev.jxbrowser.frame.Frame)5 JsObject (com.teamdev.jxbrowser.js.JsObject)5 FrameLoadFinished (com.teamdev.jxbrowser.navigation.event.FrameLoadFinished)5 BrowserView (com.teamdev.jxbrowser.view.javafx.BrowserView)5 WindowAdapter (java.awt.event.WindowAdapter)5 WindowEvent (java.awt.event.WindowEvent)5 Scene (javafx.scene.Scene)5 BrowserView (com.teamdev.jxbrowser.view.swt.BrowserView)4 OFF_SCREEN (com.teamdev.jxbrowser.engine.RenderingMode.OFF_SCREEN)3 Navigation (com.teamdev.jxbrowser.navigation.Navigation)3