Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 566877
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T13:01:27+00:00 2026-05-13T13:01:27+00:00

In Javascript, I’m making an SVG shape and adding a click handler to it

  • 0

In Javascript, I’m making an SVG shape and adding a click handler to it like this:

  var rect = document.createElementNS('http://www.w3.org/2000/svg','rect');
  rect.addEventListener('click', myClickHandler, false);

This works great. I’m trying to make an overlay Rect class in GWT. If possible, I’d like to simply do something like this:

public class SVGRect extends JavaScriptObject {

    public native void addClickHandler(ClickHandler handler) /*-{
        addEventListener('click', handler, false);
    }-*/;
}

This way I can pass a ‘normal’ GWT handler to this class, and use it from the outside as any other normal GWT UI element. I’m not sure how to hook up the ClickHandler object to the native javascript implementation of the object though?

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T13:01:27+00:00Added an answer on May 13, 2026 at 1:01 pm

    Because you need a ClickEvent to pass to ClickHandler.onClick and obtaining it from JavaScript poses a problem (AFAICT) – I’d go with a slightly different, more “generic” approach:

    Create a simple Callback interface:

    public interface Callback {
        void execute();
    }
    

    You can then pass it directly and call it like this:

    public native void addClickHandler(Callback callback) /*-{
            addEventListener('click', function() {
               callback.@path.to.your.package.Callback::execute()();
            }, false);
    }-*/;
    

    Or create an intermediate step:

    public void addClickHandler(Callback callback) {
        _addClickHandler(getCallback(callback));
    }
    
    private native void _addClickHandler(JavaScriptObject callback) /*-{
         addEventListener('click', callback, false);
    }-*/;
    
    // This can be moved to a better place
    private native static JavaScriptObject getCallback(Callback callback) /*-{
        return function()
        {
            callback.@path.to.your.package.Callback::execute()();
        };
    }-*/;
    

    And, of course, you’d use it like this:

    SVGRect svg = getMeSVGRect();
    svg.addClickHandler(new Callback() {
        @Override
        public void execute() {
            // Do some stuff
        }
    });
    

    Personally, I prefer the second solution – while it’s more code, I like to keep my native/JSNI methods private (unless it’s an overlay object or something similar) and the code is more readable and less error-prone (no need to use the funky syntax to call Java functions from inside native/JSNI code).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 379k
  • Answers 379k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer That picture looks like just a bunch of lines drawn… May 14, 2026 at 9:30 pm
  • Editorial Team
    Editorial Team added an answer You could make the j_password field hidden and then when… May 14, 2026 at 9:29 pm
  • Editorial Team
    Editorial Team added an answer I'd guess that not being attached to a tty, the… May 14, 2026 at 9:29 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.