Swt Browser & Eclipse

Eclipse: The SWT browser widget could not be created

From the error message and from some further research it is clear, that the SWT browser widget relies on the XULRunner software to render HTML. As the error message states, this hasn't been ported to GTK3 (yet) and can thus not be used. Per default eclipse does start in GTK3-mode though, so that's the root problem.

An attempt to solve it could be to force eclipse into GTK2-mode (see here) but further investigation of the Eclipse FAQ shows, that from Eclipse 4.8 and onward XULRunner isn't supported at all anymore.

Therefore the option to use the SWT.MOZILLA style for creating the browser seems to be invalid in those versions. Instead one has to focus on getting the SWT.WEBKIT style to work. For that another visit in the FAQ reveals that

WebKitGTK 1.2.0 or newer must be in the library load path.

Therefore I went ahead and searched for the respective library and installed it (libwebkitgtk-3.0-0 in my case).

After having installed that package above SWT-snipped started just fine and after having restarted eclipse, the JavaDoc was back to full functionality.

If this doesn't work for you directly, you might have to set the org.eclipse.swt.browser.DefaultType variable accordingly. According to this question, this can be done by ensuring that -Dorg.eclipse.swt.browser.DefaultType=webkit is in the eclipse.ini file.

SWT Browser in eclipse plugin behaves differently in Mac and Windows

This works just fine here on Windows 7 using a Browser with SWT.NONE:

public static void main(String[] args)
{
Display display = new Display();

final Shell shell = new Shell(display);
shell.setText("StackOverflow");
shell.setLayout(new FillLayout());

Browser browser = new Browser(shell, SWT.NONE);

String url = "start.html";
try
{
URI uri = new File(url).toURI();
URL urls = uri.toURL();
browser.setUrl(urls.toString());
}
catch (MalformedURLException e)
{
e.printStackTrace();
}

shell.pack();
shell.setSize(400, 300);
shell.open();

while (!shell.isDisposed())
{
if (!display.readAndDispatch())
{
display.sleep();
}
}

display.dispose();
}

This is the content of the start.html:

<a href="./test.html">Click here</a>

And the content of test.html:

<h1>HELLO</h1>

Why is the eclipse swt browser not focused?

I solved this by passing the shell rather than display. I still can't find the appropriate swt documentation for why this works but anyway, here's what my solution looks like now:

public class PreferencePage extends FieldEditorPreferencePage implements IWorkbenchPreferencePage {

AuthenticationManager authenticationManager = new AuthenticationManager();

@Override
protected void createFieldEditors() {
final Button login = new Button(getFieldEditorParent(), SWT.PUSH);
login.setText("Login")
login.addSelectionListener(new SelectionListener() {
@Override
public void widgetSelected(final SelectionEvent se) {
authenticationManager.run(getShell());
}
}
}
}

public AuthenticationManager {
public void run(@Nullable Shell optionalShell) {
final Display display;
if (optionalShell == null) {
if (PlatformUI.isWorkbenchRunning()) {
display = PlatformUI.getWorkbench().getDisplay();
} else {
display = null;
}
} else {
display = optionalShell.getDisplay();
}


display.syncExec(() -> {
// set the url & add listeners
}
}
}

How to read cookie from org.eclipse.swt.browser.Browser?

Try getting the cookie from JavaScript instead of the Browser#getCookie() method. It worked for me during my test, but as I don't know your website, I can't test it against it:

public static void main(String[] args)
{
Display display = new Display();

Shell shell = new Shell(display);
shell.setText("StackOverflow");
shell.setLayout(new GridLayout());

final Browser browser = new Browser(shell, SWT.NONE);
browser.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

final String url = "https://...";
browser.setUrl(url);

/* Define the function to call from JavaScript */
new BrowserFunction(browser, "cookieCallback") {
@Override
public Object function(Object[] objects) {

Object[] keyValuePairs = (Object[]) objects[0];

for(Object keyValue : keyValuePairs)
{
Object[] pair = (Object[]) keyValue;

if(Objects.equals("JSESSIONID", pair[0]))
System.out.println(pair[1]);
}

return null;
}
};

Button button = new Button(shell, SWT.PUSH);
button.setText("Get cookie");
button.addListener(SWT.Selection, new Listener() {
@Override
public void handleEvent(Event event) {
/* Get the cookie from JavaScript and then call the function */
browser.execute("cookieCallback(document.cookie.split( ';' ).map( function( x ) { return x.trim().split( '=' ); } ));");
}
});

shell.setSize(400, 300);
shell.open();

while (!shell.isDisposed())
{
if (!display.readAndDispatch())
{
display.sleep();
}
}

display.dispose();
}


Related Topics



Leave a reply



Submit