Search code examples
javaswingmouseeventkioskjxbrowser

JxBrowser - How to react on a HTML-button click in a Kiosk app without mouse-events


I want to use the JxBrowser in my Kiosk-app (based on Swing). Through its nature, no external-mouse and -keyboard are available. But I want to receive an event, if someone is clicking on a (HTML-) button at a specific website.

Originally, I have used the mouse-listener at the browser-view to receive these events. On my Mac (with an external-mouse) I received the mouse-events, but on my terminal (without an external-mouse) I do not receive any mouse-events from the browser-view. I thought that touch-events are handled like mouse-events - but unfortunately this is not the case.

I tried other event-types but without success. I also tried to add a (transparent) JButton to the browser-view. It was displayed full-screen at top of the browser-view, but without transparency.

Are there any other possibilities to receive such an event?


Solution

  • In general you need to follow these steps:

    1. Wait until a web page is loaded - listen to browser load events: browser.addLoadListener(new YourLoadAdapter()

    2. Search for all clickable HTML elements on the page (links, buttons, ...), use simple DOM API or more complex XPath expression:
      List<DOMElement> hyperlinks = document.findElements(By.tagName("a"));

    3. Attach a listener on each of them:
      element.addEventListener(DOMEventType.OnClick, new YourDOMEventListener());

    I used JxBrowser for a very similar application a few years ago, on that time the JxBrowser was based on Mozilla. And based on that experience I'm not a big fan of embedding external web browser into Java application. The same behaviour can be implemented as standard Chrome app or extension with less maintenance problems and unexpected behaviour.