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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…