SwingWorker in Java (beginner question)

Posted by Malachi on Stack Overflow See other posts from Stack Overflow or by Malachi
Published on 2010-03-24T23:30:47Z Indexed on 2010/03/24 23:33 UTC
Read the original article Hit count: 330

Filed under:
|
|

I am relatively new to multi-threading and want to execute a background task using a Swingworker thread - the method that is called does not actually return anything but I would like to be notified when it has completed.

The code I have so far doesn't appear to be working:

private void crawl(ActionEvent evt)
{
    try
    {
    SwingWorker<Void, Void> crawler = new SwingWorker<Void, Void>()
    {
        @Override
        protected Void doInBackground() throws Exception
        {
            Discoverer discover = new Discoverer();
            discover.crawl();
            return null;
        }

        @Override
        protected void done()
        {
            JOptionPane.showMessageDialog(jfThis, "Finished Crawling", "Success", JOptionPane.INFORMATION_MESSAGE);
        }

    };
    crawler.execute();
    }
    catch (Exception ex)
    {
         JOptionPane.showMessageDialog(this, ex.getMessage(), "Exception", JOptionPane.ERROR_MESSAGE);
    }
}

Any feedback/advice would be greatly appreciated as multi-threading is a big area of programming that I am weak in.

© Stack Overflow or respective owner

Related posts about java

Related posts about multithreading