user2064791
user2064791

Reputation:

Running UI thread indefinitely

I am writing a piece of code using Java Swing. Basically what it does is that it processes some lengthy task. While the task is running, I want to have a waiting pop-up window with a GIF image in it.

My question is that

        final InfoDialog infoDialog = new InfoDialog("Parsing file: " + fileToBeUploaded.getName());

        final File finalFileToBeUploaded = fileToBeUploaded;

        class FileParsingWorker extends SwingWorker<Void, String> {            
            @Override
            protected Void doInBackground() throws Exception {
                String text = fileParsers.parseFile(finalFileToBeUploaded);
                publish(text);
                return null;
            }

            @Override
            protected void process(List<String> chunks) {
                infoDialog.setVisible(false);
            }
        }

        infoDialog.setVisible(true);
        FileParsingWorker fileParsingWorker = new FileParsingWorker();
        fileParsingWorker.execute();

The InfoDialog is the small UI pop-up window with a GIF animation in it. Basically, I put the lengthy task in the worker but the UI's setVisibles in two places. I am thinking if there is any ways I can run the InfoDialog UI in a thread so that I can reuse that bit of code?

The problem I have is that I want to try to run the InfoDialog indefinitely until I deliberately stop it. If I put setVisible(true) in a thread, that thread immediately terminates and my UI won't be updated.

Can someone show me how to do this?

Upvotes: 2

Views: 123

Answers (1)

David Kroukamp
David Kroukamp

Reputation: 36423

Please have a read on Concurrency in Swing specifically The Event Dispatch Thread. This is the thread on which all Swing components should be created and manipulated. i.e:

SwingUtilities.invokeLater(new Runnable () {
    @Override
    public void run() { 
        final InfoDialog infoDialog = new InfoDialog("Parsing file: " + fileToBeUploaded.getName());

        final File finalFileToBeUploaded = fileToBeUploaded;

        ...

        infoDialog.setVisible(true);
        FileParsingWorker fileParsingWorker = new FileParsingWorker();
        fileParsingWorker.execute();
    }
});

Also I think another problem is you set the dialog back to invisible in overriden process(List<String> chunks) of the Swing worker, thus as the first chunk is read the dialog will be closed. I think Swing Workers done() method might be more what you want, and its executed on EDT:

class FileParsingWorker extends SwingWorker<Void, String> {            
    @Override
    protected Void doInBackground() throws Exception {
        String text = fileParsers.parseFile(finalFileToBeUploaded);
        publish(text);
        return null;
    }

    @Override
    protected void process(List<String> chunks) {
      //each chunk will get processed here
    }

    @Override
    protected void done() {//when Swing worker is finished this method is called
        infoDialog.setVisible(false);
    }
}

Upvotes: 3

Related Questions