Search in sources :

Example 66 with Browser

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

the class PrintToPdf method main.

public static void main(String[] args) {
    Engine engine = Engine.newInstance(OFF_SCREEN);
    Browser browser = engine.newBrowser();
    browser.set(PrintCallback.class, (params, tell) -> tell.print());
    browser.set(PrintHtmlCallback.class, (params, tell) -> {
        PdfPrinter<PdfPrinter.HtmlSettings> pdfPrinter = params.printers().pdfPrinter();
        PrintJob<HtmlSettings> printJob = pdfPrinter.printJob();
        printJob.settings().pdfFilePath(Paths.get("google.pdf").toAbsolutePath()).enablePrintingBackgrounds().orientation(PORTRAIT).apply();
        printJob.on(PrintCompleted.class, event -> {
            if (event.isSuccess()) {
                System.out.println("Printing is completed successfully.");
            } else {
                System.out.println("Printing has failed.");
            }
        });
        tell.proceed(pdfPrinter);
    });
    browser.navigation().loadUrlAndWait("https://google.com");
    browser.mainFrame().ifPresent(Frame::print);
}
Also used : Frame(com.teamdev.jxbrowser.frame.Frame) HtmlSettings(com.teamdev.jxbrowser.print.PdfPrinter.HtmlSettings) Engine(com.teamdev.jxbrowser.engine.Engine) Browser(com.teamdev.jxbrowser.browser.Browser)

Example 67 with Browser

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

the class SaveWebPage 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("Save Web Page");
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.add(view, BorderLayout.CENTER);
        frame.setSize(700, 500);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    });
    browser.navigation().loadUrlAndWait("https://www.google.com");
    Path file = Paths.get("index.html");
    Path dir = Paths.get("resources_dir");
    if (browser.saveWebPage(file, dir, SavePageType.COMPLETE_HTML)) {
        System.out.println("The web page has been saved to " + file.toAbsolutePath() + "\n" + "The resources has been saved to " + dir.toAbsolutePath() + " directory");
    } else {
        System.err.println("Failed to save the web page to " + file);
    }
}
Also used : Path(java.nio.file.Path) JFrame(javax.swing.JFrame) BrowserView(com.teamdev.jxbrowser.view.swing.BrowserView) Engine(com.teamdev.jxbrowser.engine.Engine) Browser(com.teamdev.jxbrowser.browser.Browser)

Example 68 with Browser

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

the class JarProtocol method main.

public static void main(String[] args) {
    Engine engine = Engine.newInstance(EngineOptions.newBuilder(HARDWARE_ACCELERATED).addScheme(Scheme.JAR, new InterceptJarRequestCallback()).build());
    Browser browser = engine.newBrowser();
    invokeLater(() -> {
        BrowserView view = BrowserView.newInstance(browser);
        JFrame frame = new JFrame("JAR Protocol Handler");
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.add(view, BorderLayout.CENTER);
        frame.setSize(700, 500);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    });
    // Load the index.html file located inside a JAR archive added to this Java app classpath.
    URL resource = JarProtocol.class.getResource("/resources/index.html");
    if (resource != null) {
        browser.navigation().loadUrl(resource.toString());
    }
}
Also used : JFrame(javax.swing.JFrame) BrowserView(com.teamdev.jxbrowser.view.swing.BrowserView) Engine(com.teamdev.jxbrowser.engine.Engine) URL(java.net.URL) Browser(com.teamdev.jxbrowser.browser.Browser)

Example 69 with Browser

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

the class JsAccessibleMethod 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 Java object into JavaScript and associate it
                // with the "window.java" JavaScript property.
                jsObject.putProperty("java", new JavaObject());
            }
            // Call the annotated public method of the injected Java object from JS.
            frame.executeJavaScript("window.java.sayHello()");
        });
    }
}
Also used : JsObject(com.teamdev.jxbrowser.js.JsObject) Engine(com.teamdev.jxbrowser.engine.Engine) Browser(com.teamdev.jxbrowser.browser.Browser)

Example 70 with Browser

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

the class JsConsoleEvents 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("JS Console Listener");
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.add(view, BorderLayout.CENTER);
        frame.setSize(700, 500);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    });
    browser.on(ConsoleMessageReceived.class, event -> {
        ConsoleMessage consoleMessage = event.consoleMessage();
        System.out.printf("Level: %s\nMessage: %s%n", consoleMessage.level().name(), consoleMessage.message());
    });
    browser.mainFrame().ifPresent(frame -> frame.executeJavaScript("console.error(\"Error message\");"));
}
Also used : JFrame(javax.swing.JFrame) BrowserView(com.teamdev.jxbrowser.view.swing.BrowserView) Engine(com.teamdev.jxbrowser.engine.Engine) ConsoleMessage(com.teamdev.jxbrowser.js.ConsoleMessage) 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