user2649568
user2649568

Reputation: 11

Swing Action Listener Called Multiple Times - Hyper Sensitive

I have a simple action listener in a Swing app in Java Web Start (believe me it works great 99.99% of the time) that is getting called multiple times from the UI. Here is the scenario

I am thinking there could be 2 hypotheses

I have seen the multiple click action in the case of HTML buttons in a browser and handled it with javascript/jquery. Does Swing have a similar hyper sensitivity?

I would like to use Eclipse debugger to diagnose thread problems, but don't know how to simulate this scenario. Stopping one thread is tripping the whole thing from launching another thread.

In any event, the Swing application is hyper sensitive, if anything. It does the job of passing the payload to the DB, but it is doing multiple times in 1% of the cases. I can write some DB code to filter the data, but I prefer a fix in Swing, if that is the root cause.

I tried to recreate the bug scenario with Sikuli by sending multiple clicks to the button but in vain. Can you suggest me a way to simulate this possible UI/Threading bug scenario so that I can apply relevant fix? This has to be feasible as Swing has been around for decades now and lots of legacy apps use Swing and EJB.

Your help will be gratefully acknowledged.

Thank you

Upvotes: 0

Views: 255

Answers (1)

camickr
camickr

Reputation: 324128

the action listener calls a server side (EJB) method which populates some tables in the database

A long running task should not execute on the Event Dispatch Thread (EDT). So I would suggest that when you click your button you would:

  1. disable the button to indicate it is processing the task
  2. start a SwingWorker to update the database on a separate thread.
  3. when the SwingWorker finishes executing you enable the button.

This would prevent the user from clicking the button twice while the task is running.

Read the section from the Swing tutorial on Concurrency for more information on the EDT and on the SwingWorker.

Upvotes: 1

Related Questions