Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Relative coordinates of a click in Vaadin

Tags:

java

vaadin

I'm building an app with Vaadin 14 where the user is required to click an image and the coordinates from that click are then further processed. Vaadin seems to only offer click coordinates relative to the user's screen or browser. My app needs the coordinates to be relative to the component. Is there a way to accomplish this?

like image 476
tuomasss Avatar asked Oct 12 '25 16:10

tuomasss


1 Answers

The answer was to go through the Element API and use image.getElement().addEventListener("click", ...).addEventData(...) and thus pass the necessary info from browser to the server.

In my case:

Image image = ...
image.getElement().addEventListener("click", this::handleClick)
                .addEventData("event.offsetX")
                .addEventData("event.offsetY");

private void handleClick(DomEvent event) {
        JsonObject eventData = event.getEventData();
        double x = eventData.getNumber("event.offsetX");
        double y = eventData.getNumber("event.offsetY");

        String text = "X: " + x + ", Y: " + y;
        System.out.println(text);
    }

More info: https://vaadin.com/docs/v14/flow/element-api/tutorial-event-listener

like image 73
tuomasss Avatar answered Oct 14 '25 05:10

tuomasss



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!