Reputation: 2979
I have implemented sample swt browser application, it's working in windows operating system, but the same code I have tested in linux operating system, browser is opening but window.close()
function is not working in linux. How to fix this issue?
Sample code
public class AdvancedBrowser
{
public static void main(String[] args)
{
Display display = new Display();
Shell shell = new Shell(display);
Browser browser = new Browser(shell, SWT.NONE);
browser.setBounds(5, 5, 600, 600);
browser.addCloseWindowListener(new CloseWindowListener()
{
public void close(WindowEvent event)
{
System.out.println("closing");
Browser browser = (Browser) event.widget;
Shell shell = browser.getShell();
shell.close();
}
});
browser.setText("<a href=\"javascript:window.close();\">Close this Window</a>");
shell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
Upvotes: 0
Views: 636
Reputation: 36894
Keep in mind that window.close()
is not allowed in all browsers. Internet Explorer (which is used on Windows when you use SWT.NONE
) allows scripts to close browser windows (although it may show a prompt).
Chrome and Firefox (tested on Windows and Linux) won't allow scripts to close the window.
Since you can't really use IE in SWT on Linux, I can't think of a way to make window.close()
work.
However, you can call Java code from JavaScript within the SWT Browser
:
private static Browser browser;
public static void main(String[] args)
{
Display display = new Display();
Shell shell = new Shell(display);
browser = new Browser(shell, SWT.NONE);
browser.setBounds(5, 5, 600, 600);
browser.addListener(SWT.Close, new Listener()
{
@Override
public void handleEvent(Event event)
{
System.out.println("closing");
Browser browser = (Browser) event.widget;
Shell shell = browser.getShell();
shell.close();
}
});
new CustomFunction(browser, "theJavaFunction");
browser.setText("<a href=\"javascript:theJavaFunction();\">Close this Window</a>");
shell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
private static class CustomFunction extends BrowserFunction
{
CustomFunction(Browser browser, String name)
{
super(browser, name);
}
@Override
public Object function(Object[] arguments)
{
System.out.println("theJavaFunction() called from javascript");
Shell shell = browser.getShell();
shell.close();
return null;
}
}
There's a nice tutorial by Vogella here.
Upvotes: 1