Search in sources :

Example 41 with Browser

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

the class BitmapToSwtImage method main.

public static void main(String[] args) {
    Display display = new Display();
    try (Engine engine = Engine.newInstance(OFF_SCREEN)) {
        Browser browser = engine.newBrowser();
        // Resize browser to the required dimension
        browser.resize(1024, 768);
        // Load the required web page and wait until it is loaded completely
        browser.navigation().loadUrlAndWait("https://www.google.com");
        Bitmap bitmap = browser.bitmap();
        // Convert the bitmap to org.eclipse.swt.graphics.Image
        Image image = BitmapImage.toToolkit(display, bitmap);
        // Save the image to a PNG file
        ImageLoader loader = new ImageLoader();
        loader.data = new ImageData[] { image.getImageData() };
        loader.save("bitmap.png", SWT.IMAGE_PNG);
    }
    display.dispose();
}
Also used : Bitmap(com.teamdev.jxbrowser.ui.Bitmap) BitmapImage(com.teamdev.jxbrowser.view.swt.graphics.BitmapImage) Image(org.eclipse.swt.graphics.Image) ImageLoader(org.eclipse.swt.graphics.ImageLoader) Engine(com.teamdev.jxbrowser.engine.Engine) Display(org.eclipse.swt.widgets.Display) Browser(com.teamdev.jxbrowser.browser.Browser)

Example 42 with Browser

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

the class DefaultOpenPopupCallback method on.

@Override
public Response on(Params params) {
    Browser browser = params.popupBrowser();
    try {
        Display display = Display.getDefault();
        display.asyncExec(() -> {
            Shell shell = new Shell(display);
            shell.setLayout(new FillLayout());
            BrowserView view = BrowserView.newInstance(shell, browser);
            updateBounds(shell, view, params.initialBounds());
            shell.addDisposeListener(event -> {
                if (!browser.isClosed()) {
                    asyncExec(shell, browser::close);
                }
            });
            browser.on(TitleChanged.class, event -> asyncExec(shell, () -> shell.setText(event.title())));
            browser.on(BrowserClosed.class, event -> asyncExec(shell, shell::dispose));
            browser.on(UpdateBoundsRequested.class, event -> asyncExec(shell, () -> updateBounds(shell, view, event.bounds())));
            view.setVisible(true);
            shell.pack();
            shell.open();
            while (!shell.isDisposed()) {
                if (!display.readAndDispatch()) {
                    display.sleep();
                }
            }
        });
    } catch (SWTException ignore) {
        Response.proceed();
    }
    return Response.proceed();
}
Also used : Shell(org.eclipse.swt.widgets.Shell) SWTException(org.eclipse.swt.SWTException) BrowserView(com.teamdev.jxbrowser.view.swt.BrowserView) FillLayout(org.eclipse.swt.layout.FillLayout) Browser(com.teamdev.jxbrowser.browser.Browser) Display(org.eclipse.swt.widgets.Display)

Example 43 with Browser

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

the class SwtBrowserView method main.

public static void main(String[] args) {
    Engine engine = Engine.newInstance(HARDWARE_ACCELERATED);
    Browser browser = engine.newBrowser();
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("SWT BrowserView");
    shell.setLayout(new GridLayout());
    com.teamdev.jxbrowser.view.swt.BrowserView view = BrowserView.newInstance(shell, browser);
    view.setSize(700, 500);
    shell.pack();
    shell.open();
    browser.navigation().loadUrl("https://www.google.com");
    while (!shell.isDisposed()) {
        if (!display.readAndDispatch()) {
            display.sleep();
        }
    }
    engine.close();
    display.dispose();
}
Also used : Shell(org.eclipse.swt.widgets.Shell) GridLayout(org.eclipse.swt.layout.GridLayout) BrowserView(com.teamdev.jxbrowser.view.swt.BrowserView) Engine(com.teamdev.jxbrowser.engine.Engine) Browser(com.teamdev.jxbrowser.browser.Browser) Display(org.eclipse.swt.widgets.Display)

Example 44 with Browser

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

the class XPath method main.

public static void main(String[] args) {
    Engine engine = Engine.newInstance(OFF_SCREEN);
    Browser browser = engine.newBrowser();
    SwingUtilities.invokeLater(() -> {
        BrowserView view = BrowserView.newInstance(browser);
        JFrame frame = new JFrame("Evaluate XPath");
        frame.getContentPane().add(view, BorderLayout.CENTER);
        frame.setSize(800, 600);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    });
    Navigation navigation = browser.navigation();
    navigation.on(FrameLoadFinished.class, event -> event.frame().document().flatMap(Document::documentElement).ifPresent(element -> {
        try {
            XPathResult result = element.evaluate("count(//div)");
            if (result.isNumber()) {
                System.out.println("Result: " + result.asNumber());
            }
        } catch (XPathException e) {
            System.out.println(e.getMessage());
        }
    }));
    navigation.loadUrl("https://www.teamdev.com/jxbrowser");
}
Also used : XPathResult(com.teamdev.jxbrowser.dom.XPathResult) Navigation(com.teamdev.jxbrowser.navigation.Navigation) SwingUtilities(javax.swing.SwingUtilities) FrameLoadFinished(com.teamdev.jxbrowser.navigation.event.FrameLoadFinished) Document(com.teamdev.jxbrowser.dom.Document) OFF_SCREEN(com.teamdev.jxbrowser.engine.RenderingMode.OFF_SCREEN) Browser(com.teamdev.jxbrowser.browser.Browser) XPathException(com.teamdev.jxbrowser.dom.XPathException) BorderLayout(java.awt.BorderLayout) JFrame(javax.swing.JFrame) BrowserView(com.teamdev.jxbrowser.view.swing.BrowserView) Engine(com.teamdev.jxbrowser.engine.Engine) Navigation(com.teamdev.jxbrowser.navigation.Navigation) JFrame(javax.swing.JFrame) XPathException(com.teamdev.jxbrowser.dom.XPathException) BrowserView(com.teamdev.jxbrowser.view.swing.BrowserView) XPathResult(com.teamdev.jxbrowser.dom.XPathResult) Document(com.teamdev.jxbrowser.dom.Document) Engine(com.teamdev.jxbrowser.engine.Engine) Browser(com.teamdev.jxbrowser.browser.Browser)

Example 45 with Browser

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

the class ZoomLevel 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 Zoom Level");
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.add(view, BorderLayout.CENTER);
        frame.setSize(700, 500);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    });
    // Listen to the zoom changed events.
    ZoomLevels levels = engine.zoomLevels();
    levels.on(ZoomLevelChanged.class, event -> System.out.println("Url: " + event.host() + "\n" + "Zoom level: " + event.level()));
    Navigation navigation = browser.navigation();
    navigation.on(FrameLoadFinished.class, event -> {
        if (event.frame().isMain()) {
            browser.zoom().level(com.teamdev.jxbrowser.zoom.ZoomLevel.P_200);
        }
    });
    navigation.loadUrl("https://www.google.com");
}
Also used : Navigation(com.teamdev.jxbrowser.navigation.Navigation) JFrame(javax.swing.JFrame) BrowserView(com.teamdev.jxbrowser.view.swing.BrowserView) ZoomLevels(com.teamdev.jxbrowser.zoom.ZoomLevels) 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