Search code examples
javaswingswingworker

How to make a SwingWorker Start more than once..?


As in the image I have created a button called "filter" to filter out a specific data set according to their current status.I used a SwingWorker to retrieve data from the database.The code that I have now works fine. once I click the button it shows the related data. But when I click it again with different status, it does not show the required data.I am reading that the doingBackground method will only be executed once. Is there a way to fire it off again..??

enter image description here


Solution

  • You can't restart a SwingWorker, and what's more, you don't want to. Per the SwingWorker API:

    SwingWorker is only designed to be executed once. Executing a SwingWorker more than once will not result in invoking the doInBackground method twice.

    Your solution is to create a new SwingWorker instance within your button's ActionListener and execute it; do this, and you'll likely be fine.