Skip to content Skip to sidebar Skip to footer

Search String In SWT Webbrowser Widget

I wrote a program which includes the browser widget from swt. Now I navigate to google and want to search for the string 'Pictures' for example. This is my code but it doesn't work

Solution 1:

Here is a very simple example, that will return a value from JavaScript to Java and print it to the command line:

public static void main(String[] args)
{
    Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setText("StackOverflow");
    shell.setLayout(new FillLayout());

    final Browser browser = new Browser(shell, SWT.NONE);
    browser.setText("......baz");

    Button b = new Button(shell, SWT.PUSH);
    b.setText("Do something");
    b.addListener(SWT.Selection, new Listener()
    {
        public void handleEvent(Event e)
        {
            String baz = "baz";
            boolean result = (boolean) browser.evaluate("return window.find('" + baz + "');");
            System.out.println(result);
        }
    });

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

Note that I used a different JS function than you did and that you have an additional closing bracket in your JS code.

Here is an excellent tutorial.


Post a Comment for "Search String In SWT Webbrowser Widget"