czArek czArek - 28 days ago 7
HTML Question

The fastest way to perform a click in Java

I used SWT Browser in my Java project.

I get content of page by JavaScript, next get button which I need to perform to click by Java code.

Now I do this from Java code like below:

browser.execute("document.getElementById('actionButton').click();");


browser
is object from lib
org.eclipse.swt.browser.Browser;


But I am not sure that is the fastest way to click. I care as soon as possible to click on button.

Is there any fastest way to perform click on button ?

Away from graphic click, maybe by fire event ?

I'm comparing it by example when I call method
buttonComponent.doClick();
on button in Java it cost about 100ms (by graphics thread and do click on button) and when I used
buttonComponent.getActionListeners()[0].actionPerformed(null);
it cost 0ms !

Baz Baz
Answer

I'd recomment to just go with click().

You're analogy with the Swing listener is flawed, because it's not as easy as you make it out to be:

  1. What if there's no action listener at all?
  2. By passing null you loose information, etc.

I don't think the gain in performance balances out the downsides.

The same will be the case for Javascript. There are too many things that can go wrong by trying to invoke listeners (if exist) manually and the performance improvement (if any) you get from it isn't that great either.

Comments