JPanel webBrowserPanel = new JPanel(new BorderLayout());
webBrowserPanel.setBorder(BorderFactory.createTitledBorder("Native Web Browser component"));
JWebBrowser webBrowser = new JWebBrowser();
webBrowser.setBarsVisible(false);
final JTextArea functionCallsTextArea = new JTextArea(7, 0);
webBrowser.registerFunction(new WebBrowserFunction("invokeJavaNoError") {
@Override
public Object invoke(JWebBrowser webBrowser, Object... args) {
StringBuilder sb = new StringBuilder();
sb.append("-> " + getName() + "() called from Javascript with args:");
for (int i=0; i<args.length; i++) {
Object arg = args[i];
if (arg == null) {
sb.append(LS).append(" null");
} else {
sb.append(LS).append(" ").append(arg instanceof Object[]? Arrays.deepToString((Object[])arg): arg.toString()).append(" (").append(arg.getClass().getSimpleName()).append(")");
}
}
sb.append(LS).append("-> return Object[] result:").append(LS).append(" (short)3").append(LS).append(" true").append(LS).append(" null").append(LS).append(" new Object[] {\"A String\", false}").append(LS).append(" \"Hello World!\"").append(LS).append(" 2.0f / 3.0f");
functionCallsTextArea.setText(sb.toString());
functionCallsTextArea.setCaretPosition(0);
return new Object[] {(short)3, true, null, new Object[] {"A String", false}, "Hello World!", 2.0f / 3.0f};
}
});
webBrowser.registerFunction(new WebBrowserFunction("invokeJavaWithError") {
@Override
public Object invoke(JWebBrowser webBrowser, Object... args) {
functionCallsTextArea.setText("-> " + getName() + "() called from Javascript." + LS + "-> Generating a Java runtime exception.");
functionCallsTextArea.setCaretPosition(0);
// This will generate an error