Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jxBrowser error class don't exist

Tags:

jxbrowser

import com.teamdev.jxbrowser.chromium.Browser;
import com.teamdev.jxbrowser.chromium.BrowserFunction;
import com.teamdev.jxbrowser.chromium.JSValue;
import com.teamdev.jxbrowser.chromium.LoggerProvider;
import com.teamdev.jxbrowser.chromium.swing.BrowserView;

import javax.swing.*;
import java.awt.*;
import java.util.logging.Level;
import java.util.logging.Logger;

public class JavaScriptJavaSample {
  public static void main(String[] args) {
    LoggerProvider.setLevel(Level.OFF);
    Browser browser = new Browser();
    BrowserView browserView = new BrowserView(browser);

    browser.registerFunction("MyFunction", new BrowserFunction() {
        public JSValue invoke(JSValue... args) {
            System.out.println("MyFunction is invoked!");
            return JSValue.create("Hello!");
        }
    });

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    frame.add(browserView, BorderLayout.CENTER);
    frame.setSize(700, 500);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);

    browser.loadHTML("<html><body><a href='#' onclick='MyFunction();'>Call Java method</a></body></html>");
}
}

i've add all jar file,but BrowserFunction doesn't exist, why?

all other classes work perfectly , and do not understand the problem , I

imported all the jar thanks to all in advance

like image 454
Francesco Taioli Avatar asked Mar 28 '26 12:03

Francesco Taioli


1 Answers

As far as i know, using BrowserFunction() to register function is gone in new versions of jxbrowser. There is a new way in 6.1 version described here. Putting the new way in your code :

import com.teamdev.jxbrowser.chromium.Browser;
import com.teamdev.jxbrowser.chromium.BrowserFunction;
import com.teamdev.jxbrowser.chromium.JSValue;
import com.teamdev.jxbrowser.chromium.LoggerProvider;
import com.teamdev.jxbrowser.chromium.swing.BrowserView;

import javax.swing.*;
import java.awt.*;
import java.util.logging.Level;
import java.util.logging.Logger;

public class JavaScriptJavaSample {
    public static void main(String[] args) {
        LoggerProvider.setLevel(Level.OFF);
        Browser browser = new Browser();
        BrowserView browserView = new BrowserView(browser);

        browser.addLoadListener(new LoadAdapter() {
            @Override
            public void onFinishLoadingFrame(FinishLoadingEvent event) {
                if (event.isMainFrame()) {
                    Browser browser = event.getBrowser();
                    JSValue value = browser.executeJavaScriptAndReturnValue("window");
                    value.asObject().setProperty("java", new Events());
                }
            }
        });

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.add(browserView, BorderLayout.CENTER);
        frame.setSize(700, 500);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);

        browser.loadHTML("<html><body><a href='#' onclick='alert(java.MyFunction());return false;'>Call Java method</a></body></html>");
    }
}

Create another file to hold Events class :

public class Events {

    public String MyFunction() {

        System.out.println("MyFunction is invoked!");

        return new String("Hello");

    }

}

* Added return false to the end of onclick attribute of a tag, preventing browser from following the link href on click. BTW, I suggest you to use main documentation codes, as they are up-to-date.

like image 105
Mehran Torki Avatar answered Apr 02 '26 20:04

Mehran Torki



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!