2

I don't know what I'm doing wrong, but I've looked at countless examples and can't seem to get it right. I have an application which extends JFrame and at the end of the constructor (just for testing, it won't stay there) I have the following code:

    SwingWorker<Boolean, Integer> worker = new SwingWorker<Boolean, Integer>(){
        protected Boolean doInBackground() throws Exception{

            for(int i=0; i<1000000; i++) {
                publish(i);
            }

            return true;
        }

        protected void process(List<Integer> chunks) {

            for(int n : chunks) {
                System.out.println(n);
            }


        }

        protected void done() {
            boolean status = false;
            try {
                status = get();
            }catch(Exception e) {

            }
        }

    };

    worker.execute();

This is just to get a working example, eventually it will be to process tens of thousands of files which could take an estimate 30min to complete. So as such, I want it to run in the background. However, the application won't respond while it's running. Only thing I can do is move the window. What am I doing wrong?

Phaelax z
  • 1,814
  • 1
  • 7
  • 19
  • Have you tried calling `worker.execute` in a `windowOpened` method of a `WindowListener` instead of the `JFrame`'s constructor? See [this](https://stackoverflow.com/q/13207519/1707353) for more detail. I think you need to find a more complete example demonstrating the UI thread getting stuck. This doesn't look like it's enough code to reveal the problem. – Jeff Holt Mar 19 '19 at 03:24
  • I think maybe it was all the System.out calls is what was hanging up the GUI. I took that out and replaced it with the file processing I'm doing and ran a test sample and it worked fine. – Phaelax z Mar 19 '19 at 04:38

0 Answers0