Kiran Gurijala
Kiran Gurijala

Reputation: 3

Blank eclipse window is opening when I tried to a new browser in BrowserFunction

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;
}
}

enter image description here

Upvotes: 0

Views: 518

Answers (1)

Baz
Baz

Reputation: 36904

There are two things wrong with your code:

  1. 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();
    
  2. 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

Related Questions