Referencing DOM from Java

Skip to first unread message

Aug 19, 2019, 7:00:20 AM8/19/19
to bck2brwsr

Is it possible to perform operations on the DOM from Java code?
I'm aware that communicating whit JavaScript can be done using the @JavaScriptBody annotation, but I don't see how to return a HTML element. For primitive types this is working, obviously.

Are there any classes that represent elements from the DOM? 


Aug 20, 2019, 1:37:43 AM8/20/19
to bck2brwsr
Ok, answering part of my own question here. The Dukescript libraries provided exactly this:

These can be obtained by including this dependency:

But now I'm facing the next hurdle: how to get access to the document. This doesn't work for me:

    public static void main(String... args) {
Document document = getDocument();
@JavaScriptBody(args = {}, body = "return window.document;")
private static native Document getDocument();

While the getDocument() method actually returned an object, I'm getting errors like these:

TypeError: target.URL__Ljava_lang_String_2 is not a function

Any suggestions?

Op maandag 19 augustus 2019 13:00:20 UTC+2 schreef

Aug 20, 2019, 1:44:56 AM8/20/19
to bck2brwsr
Answering my own question again :)

This does the job:

Document document = Document.$as(getDocument());

Op dinsdag 20 augustus 2019 07:37:43 UTC+2 schreef

Jaroslav Tulach

Aug 20, 2019, 7:05:12 AM8/20/19
to, bck2brwsr
Amazing solution! Thanks for giving bck2brwsr a try!

út 20. 8. 2019 v 7:44 odesílatel <> napsal:
You received this message because you are subscribed to the Google Groups "bck2brwsr" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
To view this discussion on the web, visit

Aug 21, 2019, 11:34:23 AM8/21/19
to bck2brwsr
Hi, good to see this group is alive :)

Can I ask another 2 questions?

1. How would I bind an event? In my case I want to handle a button click but the API is unclear to me. (this bytecode to JS thing is still a mystery to me ;))

I have the following code, but it gives me an error when clicked:
        HTMLButtonElement button = HTMLButtonElement.$as(document.createElement("BUTTON"));
.textContent.set("click me");
.onclick.set((event) -> {
System.out.println("clicked " + event.toString());
return null;

My guess is I have to a $as for the lambda as well. But how?

2. I know that only a subset of the Java API is available, which is understandable from a technical and practical viewpoint. But is this easily extendible? For instance java.util.function.Consumer is not available to the runtime. What would it take to implement this on bck2brwsr?

Hope that you can shine a light on my questions.


Op dinsdag 20 augustus 2019 13:05:12 UTC+2 schreef Jaroslav Tulach:
To unsubscribe from this group and stop receiving emails from it, send an email to
Reply all
Reply to author
0 new messages