Search in sources :

Example 1 with Browser

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

the class PrintSettings method main.

public static void main(String[] args) {
    Engine engine = Engine.newInstance(HARDWARE_ACCELERATED);
    Browser browser = engine.newBrowser();
    browser.set(PrintCallback.class, (params, tell) -> tell.print());
    // #docfragment "Callback"
    browser.set(PrintHtmlCallback.class, (params, tell) -> {
        // #docfragment "Configure settings"
        SystemPrinter<HtmlSettings> printer = params.printers().defaultPrinter().orElseThrow(IllegalStateException::new);
        PrintJob<HtmlSettings> printJob = printer.printJob();
        printJob.settings().paperSize(ISO_A4).colorModel(COLOR).enablePrintingBackgrounds().disablePrintingHeaderFooter().orientation(PORTRAIT).apply();
        // #enddocfragment "Configure settings"
        // #docfragment "Subscribe to PrintCompleted"
        printJob.on(PrintCompleted.class, event -> {
            if (event.isSuccess()) {
                System.out.println("Printing is completed successfully.");
            } else {
                System.out.println("Printing has failed.");
            }
        });
        // #enddocfragment "Subscribe to PrintCompleted"
        // #docfragment "Proceed"
        tell.proceed(printer);
    // #enddocfragment "Proceed"
    });
    // #enddocfragment "Callback"
    browser.navigation().loadUrlAndWait("https://google.com");
    browser.mainFrame().ifPresent(Frame::print);
}
Also used : Frame(com.teamdev.jxbrowser.frame.Frame) HtmlSettings(com.teamdev.jxbrowser.print.SystemPrinter.HtmlSettings) Engine(com.teamdev.jxbrowser.engine.Engine) Browser(com.teamdev.jxbrowser.browser.Browser)

Example 2 with Browser

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

the class RedirectLoggingToFile method main.

public static void main(String[] args) {
    Path loggingDir;
    try {
        loggingDir = Files.createTempDirectory("jxbrowser-logs");
    } catch (IOException e) {
        throw new RuntimeException("Failed to create a temporary directory", e);
    }
    Path loggingFile = loggingDir.resolve("jxbrowser.log");
    System.setProperty("jxbrowser.logging.file", loggingFile.toAbsolutePath().toString());
    System.out.println("Log file path: " + loggingFile.toAbsolutePath());
    Engine engine = Engine.newInstance(HARDWARE_ACCELERATED);
    Browser browser = engine.newBrowser();
    SwingUtilities.invokeLater(() -> {
        BrowserView view = BrowserView.newInstance(browser);
        JFrame frame = new JFrame("Redirect Logging To File");
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.add(view, BorderLayout.CENTER);
        frame.setSize(700, 500);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    });
    browser.navigation().loadUrl("https://www.google.com");
}
Also used : Path(java.nio.file.Path) JFrame(javax.swing.JFrame) BrowserView(com.teamdev.jxbrowser.view.swing.BrowserView) IOException(java.io.IOException) Engine(com.teamdev.jxbrowser.engine.Engine) Browser(com.teamdev.jxbrowser.browser.Browser)

Example 3 with Browser

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

the class SelectClientCertificate method main.

public static void main(String[] args) {
    Engine engine = Engine.newInstance(HARDWARE_ACCELERATED);
    Browser browser = engine.newBrowser();
    SwingUtilities.invokeLater(() -> {
        view = BrowserView.newInstance(browser);
        JFrame frame = new JFrame("Select Client SSL Certificate");
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.add(view, BorderLayout.CENTER);
        frame.setSize(700, 500);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    });
    browser.set(SelectClientCertificateCallback.class, (params, tell) -> {
        if (params.certificates().size() != 0) {
            List<Certificate> certificates = params.certificates();
            List<X509Certificate> x509Certificates = new ArrayList<>();
            for (Certificate certificate : certificates) {
                try {
                    x509Certificates.add(X509Certificates.of(new ByteArrayInputStream(certificate.derEncodedValue())));
                } catch (CertificateException e) {
                    e.printStackTrace();
                }
            }
            Object[] selectionValues = x509Certificates.toArray();
            Object selectedValue = JOptionPane.showInputDialog(view, String.format(DIALOG_MESSAGE, params.hostPort().host() + ":" + params.hostPort().port()), DIALOG_TITLE, PLAIN_MESSAGE, null, selectionValues, selectionValues[0]);
            if (selectedValue != null) {
                // Tell the engine which SSL certificate has been selected.
                try {
                    X509Certificate certificate = (X509Certificate) selectedValue;
                    ClientCertificate clientCertificate = ClientCertificate.of(certificate, SslPrivateKey.of(certificate.getEncoded()));
                    tell.select(clientCertificate);
                    return;
                } catch (CertificateEncodingException e) {
                    e.printStackTrace();
                }
            }
        }
        tell.cancel();
    });
    browser.navigation().loadUrl("https://client.badssl.com/");
}
Also used : ArrayList(java.util.ArrayList) CertificateException(java.security.cert.CertificateException) CertificateEncodingException(java.security.cert.CertificateEncodingException) X509Certificate(java.security.cert.X509Certificate) JFrame(javax.swing.JFrame) ByteArrayInputStream(java.io.ByteArrayInputStream) Engine(com.teamdev.jxbrowser.engine.Engine) ClientCertificate(com.teamdev.jxbrowser.net.tls.ClientCertificate) Browser(com.teamdev.jxbrowser.browser.Browser) X509Certificate(java.security.cert.X509Certificate) Certificate(com.teamdev.jxbrowser.net.tls.Certificate) ClientCertificate(com.teamdev.jxbrowser.net.tls.ClientCertificate)

Example 4 with Browser

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

the class SelectionAsHtml 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);
        JButton button = new JButton("Get Selected HTML");
        button.addActionListener(e -> browser.mainFrame().ifPresent(frame -> SwingUtilities.invokeLater(() -> JOptionPane.showMessageDialog(view, frame.selectionAsHtml(), "Selected HTML", JOptionPane.PLAIN_MESSAGE))));
        JFrame frame = new JFrame("Get Selected HTML");
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.add(button, BorderLayout.NORTH);
        frame.add(view, BorderLayout.CENTER);
        frame.setSize(800, 600);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    });
    browser.navigation().loadUrl("https://www.teamdev.com/jxbrowser#features");
}
Also used : SwingUtilities(javax.swing.SwingUtilities) JButton(javax.swing.JButton) Browser(com.teamdev.jxbrowser.browser.Browser) HARDWARE_ACCELERATED(com.teamdev.jxbrowser.engine.RenderingMode.HARDWARE_ACCELERATED) BorderLayout(java.awt.BorderLayout) JFrame(javax.swing.JFrame) JOptionPane(javax.swing.JOptionPane) BrowserView(com.teamdev.jxbrowser.view.swing.BrowserView) Engine(com.teamdev.jxbrowser.engine.Engine) WindowConstants(javax.swing.WindowConstants) JFrame(javax.swing.JFrame) JButton(javax.swing.JButton) BrowserView(com.teamdev.jxbrowser.view.swing.BrowserView) Engine(com.teamdev.jxbrowser.engine.Engine) Browser(com.teamdev.jxbrowser.browser.Browser)

Example 5 with Browser

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

the class SpellCheckEvents 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("Spell Check Events");
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.add(view, BorderLayout.CENTER);
        frame.setSize(700, 500);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    });
    browser.on(SpellCheckCompleted.class, event -> event.results().forEach(checkResult -> {
        System.out.println("Error start index: " + checkResult.location());
        System.out.println("Error length: " + checkResult.length());
    }));
    browser.mainFrame().ifPresent(mainFrame -> {
        mainFrame.loadHtml("<html><body>" + "<textarea autofocus rows='20' cols='30'>Smple text with mitake.</textarea>" + "</body></html>");
    });
}
Also used : SwingUtilities(javax.swing.SwingUtilities) Browser(com.teamdev.jxbrowser.browser.Browser) HARDWARE_ACCELERATED(com.teamdev.jxbrowser.engine.RenderingMode.HARDWARE_ACCELERATED) SpellCheckCompleted(com.teamdev.jxbrowser.browser.event.SpellCheckCompleted) BorderLayout(java.awt.BorderLayout) JFrame(javax.swing.JFrame) BrowserView(com.teamdev.jxbrowser.view.swing.BrowserView) Engine(com.teamdev.jxbrowser.engine.Engine) WindowConstants(javax.swing.WindowConstants) 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