JxBrowser supports Swing/AWT and JavaFX UI toolkits. It provides two implementations of the BrowserView class that can be used for embedding JxBrowser into Swing/AWT and JavaFX applications:

  • com.teamdev.jxbrowser.chromium.swing.BrowserView
  • com.teamdev.jxbrowser.chromium.javafx.BrowserView

To embed JxBrowser into SWT applications you can use the approaches described below. These approaches are based on the third-party SWT-AWT and SWT-JavaFX components (bridges).


Note: please note that mixing the two UI toolkits isn't a simple task, so there might be some integration issues such as focus transferring, flickering, freezing, thread deadlocks, etc. You can read more about possible issues in the Swing/SWT Integration article.


Swing & SWT

To embed Swing/AWT implementation of com.teamdev.jxbrowser.chromium.swing.BrowserView into SWT application use the org.eclipse.swt.awt.SWT_AWT instance as shown below: 

import java.awt.Frame;
 
import org.eclipse.swt.SWT;
import org.eclipse.swt.awt.SWT_AWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
 
import com.teamdev.jxbrowser.chromium.Browser;
import com.teamdev.jxbrowser.chromium.swing.BrowserView;
 
/**
 * The sample demonstrates how to use JxBrowser Swing control in 
 * SWT application using SWT_AWT bridge.
 */
public class JxBrowserSwingSWT {
    public static void main(String[] arguments) {
        Browser browser = new Browser();
        BrowserView view = new BrowserView(browser);
 
        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setLayout(new FillLayout());
         
        Composite composite = new Composite(shell, 
                SWT.EMBEDDED | SWT.NO_BACKGROUND);
        Frame frame = SWT_AWT.new_Frame(composite);
        frame.add(view);
         
        browser.loadURL("http://google.com");
         
        shell.open();
         
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
        display.dispose();
    }
}


JavaFX & SWT

To embed JavaFX implementation of com.teamdev.jxbrowser.chromium.javafx.BrowserView into SWT application use the javafx.embed.swt.FXCanvas instance as shown below: 

import javafx.embed.swt.FXCanvas;
import javafx.scene.Scene;
 
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
 
import com.teamdev.jxbrowser.chromium.Browser;
import com.teamdev.jxbrowser.chromium.javafx.BrowserView;
 
/**
 * The sample demonstrates how to use JxBrowser JavaFX 
 * control in SWT application using FXCanvas.
 */
public class JxBrowserJavaFXSWT {
    public static void main(String[] arguments) {
        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setLayout(new FillLayout());
         
        Browser browser = new Browser();
        FXCanvas canvas = new FXCanvas(shell, SWT.NONE);
         
        // BrowserView instance must be initialized after FXCanvas.
        BrowserView view = new BrowserView(browser);
        canvas.setScene(new Scene(view));
         
        browser.loadURL("http://google.com");
         
        shell.open();
        while (!shell.isDisposed()) {
               if (!display.readAndDispatch()) {
                     display.sleep();
               }
        }
        display.dispose();
    }
}