0

In my Java Swing application I have the following code that is called when a button is clicked:

class MyWorker extends SwingWorker<String, Object> {

    @Override
    protected String doInBackground() throws Exception {
        loadMasterFile();
        judgeFileListModel = new DefaultListModel();
        // Refresh model
        return "Done.";
    }

    @Override
    protected void done() {
        LB.setVisible(false);
    }
}

new MyWorker().execute();

In the called code I open a JFileChooser. The first time it runs great - If I then press the button again after the code has finished it hangs. If I then debug and pause when that happens I appear to have a deadlock.

I have no clue how to handle this - still a student.

I'd absolutely love some help if possible.

EDIT:

I've fixed it!

To anyone having a similar issue - it's due to the JFileChooser being inside the loadMasterFile() method. It apparently violates swing rules having any GUI code inside doInBackground() so I refactored it out of there and it works great.

Gladhus
  • 910
  • 1
  • 13
  • 24
user3495579
  • 23
  • 1
  • 4
  • We don't know what is happening in `loadMasterFile` but it doesn't return anything and is probably not threadsafe. – Hauke Ingmar Schmidt Apr 08 '14 at 22:46
  • @his could I make it threadsafe by making it return a value? I could make it return true for completed or something? – user3495579 Apr 08 '14 at 22:50
  • Sorry, I think I have been a little hasty here. If the method is defined in `MyWorker` itself then this is not the strong hint that I assumed. Still you seem to access objects that seem to be elsewhere (`LB`, which is a bad variable name btw). If you do so in `loadMasterFile` also this _may_ be part of the problem. You need to show us more of the context of the specific call that fails. – Hauke Ingmar Schmidt Apr 08 '14 at 23:31
  • @his I have a password dialog ask for a password - whether I select cancel or ok it carries on and doesn't show a JFileChooser. I'm calling it with showOpenDialog(null), not this - if it makes a difference – user3495579 Apr 08 '14 at 23:57
  • @his I should also mention that before I had the password asked after the JFileChooser, now it's before and not working at all on click. – user3495579 Apr 09 '14 at 00:06
  • @user3495579 [maybe ...](http://stackoverflow.com/questions/7053865/cant-get-arrayindexoutofboundsexception-from-future-and-swingworker-if-threa) – mKorbel Apr 09 '14 at 06:51

0 Answers0