user1699649
user1699649

Reputation: 1

Execute SwingWorker in a for loop

I want to acquire images from a scanner and place them in a HashMap with names in a certain order of sequence performed by SwingWorker Class called ScanningService. I want to use a for loop to acquire another image after the first is complete using the same device. scan.getImage() returns the scanned image once done. The sc parameter is a scanner object. Maybe someone can offer me an implementation with Executor or Timer because my trials have not borne any fruits.

    for(int k=0; k<10; k++){
        lblMessage.setText("Scan Next Image: ");
        scan = new ScanningService(sc);
        scan.execute();
        scan.addPropertyChangeListener(new PropertyChangeListener() {
            @Override
            public void propertyChange(PropertyChangeEvent arg0) {
                // TODO Auto-generated method stub
                if(StateValue.DONE == scan.getState()){

                    lblImagePreview.setIcon(new ImageIcon(scan.getImage()));
                    //logic for name in order of sequence
                    imageList.put<imagename, scan.getImage());
                }
            }
        });
    }

Upvotes: 0

Views: 397

Answers (1)

mKorbel
mKorbel

Reputation: 109815

  • SwingWorker is designated to run only once, then after you have to invoke a new instance

  • SwingWorker isn't proper for loading an images, nor on some period

  • have look Runnable#Thread, output to the Swing GUI must be wrapper in invokeLater()

  • all changes to the Swing GUI must be done on EventDispatchThread, you have look at Concurency in Swing

Upvotes: 1

Related Questions