Skip to content

Instantly share code, notes, and snippets.

@hallahan
Created October 14, 2013 04:40
Show Gist options
  • Save hallahan/6970864 to your computer and use it in GitHub Desktop.
Save hallahan/6970864 to your computer and use it in GitHub Desktop.
This is a very bare-bones example that demonstrates how to put an SWT Browser view into a Swing JFrame. Note that you need to download the SWT toolkit and put it in your classpath of your Java project for this to work.
import java.awt.Canvas;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
import org.eclipse.swt.SWT;
import org.eclipse.swt.awt.SWT_AWT;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class MySWTBrowserTest {
static Display display;
public static void main(String args[]) {
display = new Display();
JFrame f = new JFrame("SWT Browser");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final Canvas canvas = new Canvas();
f.setSize(650, 550);
f.getContentPane().add(canvas);
f.setVisible(true);
display.asyncExec(new Runnable() {
@Override
public void run() {
Shell shell = SWT_AWT.new_Shell(display, canvas);
shell.setSize(600, 500);
Browser browser = new Browser(shell, SWT.NONE);
browser.setLayoutData(new GridData(GridData.FILL_BOTH));
browser.setSize(600, 500);
browser.setUrl("http://leafletplayback.theoutpost.io");
shell.open();
}
});
while (true) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment