Reputation: 3
I am tries to open a new eclipse browser when the button is clicked from another eclipse browser. I am using browserfunction to open shell which will embedded the new browser but the new shell is opening but page is not loading
import org.eclipse.swt.*;
import org.eclipse.swt.browser.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;
public class Snippet {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
shell.setBounds(10, 10, 800, 800);
final Browser browser;
try {
browser = new Browser(shell, SWT.NONE);
} catch (SWTError e) {
System.out.println("Could not instantiate Browser: "
+ e.getMessage());
display.dispose();
return;
}
browser.setUrl("http://localhost:8080/SampleTest");
final BrowserFunction function = new CustomFunction(browser,
"getdatafromjava2");
browser.addProgressListener(new ProgressAdapter() {
@Override
public void completed(ProgressEvent event) {
browser.addLocationListener(new LocationAdapter() {
@Override
public void changed(LocationEvent event) {
browser.removeLocationListener(this);
System.out
.println("left java function-aware page, so disposed CustomFunction");
function.dispose();
}
});
}
});
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
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 with args:");
Display display = Display.getCurrent();
Shell shell = new Shell(display);
Browser browser2 = new Browser(shell, SWT.NONE);
// browser2.setText(createHTML ());
browser2.setUrl("http://localhost:8080/SampleTest/Test.jsp");
shell.setText("New Window");
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
return null;
}
}
Upvotes: 0
Views: 518
Reputation: 36904
There are two things wrong with your code:
You have two SWT event loops. You should only ever have one. Remove the second one.
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
Your new Shell
doesn't have a Layout
. Adding a FillLayout
to it will solve your problem:
shell.setLayout(new FillLayout());
Here's a complete example:
public class StackOverflow
{
public static void main(String[] args)
{
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
shell.setBounds(10, 10, 800, 800);
final Browser browser;
try
{
browser = new Browser(shell, SWT.NONE);
}
catch (SWTError e)
{
System.out.println("Could not instantiate Browser: "
+ e.getMessage());
display.dispose();
return;
}
browser.setText("<a href='' onclick='getdatafromjava2()'>Click here</a>");
final BrowserFunction function = new CustomFunction(browser,
"getdatafromjava2");
browser.addProgressListener(new ProgressAdapter()
{
@Override
public void completed(ProgressEvent event)
{
browser.addLocationListener(new LocationAdapter()
{
@Override
public void changed(LocationEvent event)
{
browser.removeLocationListener(this);
System.out
.println("left java function-aware page, so disposed CustomFunction");
function.dispose();
}
});
}
});
shell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
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 with args:");
Display display = Display.getCurrent();
Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
Browser browser2 = new Browser(shell, SWT.NONE);
// browser2.setText(createHTML ());
browser2.setText("SUCCESS");
shell.setText("New Window");
shell.open();
return null;
}
}
Upvotes: 1