0

I have a JFrame that does things. I have a JButton hidden from view in that JFrame. In a SwingWorker i have a utility Class such as checkNDownloadFile of which I pass a JButton to it. So it can make it visible/usable when the process completes.

My question is, is this acceptable. I dont know of any other method to do this effect. (Keep in mind the checkNDownloadFile class is all static. Its only needed/ran once.)


Sudo Code

-----------------------------------------------------------------
myWorker Class

protected Void doInBackground() throws Exception {

    //Loading time consuming data.
    //Execute Dialog of the question variety.
    //Loading more time consuming data.

    //Create JFrame
    AtomFrame frame = new AtomFrame();
    frame.start();

    checkNDownloadFile.setButton(frame.fileButton)
    checkNDownloadFile.start();
    return null;
}

-----------------------------------------------------------------
checkNDownloadFile Class

public static void start() {
    //Do the other task at hand
    if (complete && good) {
        fileButton.setVisible(true);
    } else {
        //other stuff
    }
}

Answer Code

-----------------------------------------------------------------
myWorker Class

protected Void doInBackground() throws Exception {

    //Loading time consuming data.
    //Execute Dialog of the question variety.
    //Loading more time consuming data.

    //Create JFrame
    //Moved to Main Method to be created by EDT.
    //AtomFrame frame = new AtomFrame();
    //frame.start();

    publish("Executing");
    boolean returnedB = checkNDownloadFile.start();
    if (returnedB) {
        publish("Good");
    } else {
        //Maybe implement
        //checkNDownloadFile.getError();
        publish("Bad");
    }
    return null;
}

-----------------------------------------------------------------
checkNDownloadFile Class

public static void start() {
    //Do the other task at hand
    if (complete && good) {
        return true
    } else {
        //Maybe implement
        //setError("");
        return false
    }
}
TrinaryAtom
  • 242
  • 3
  • 14

1 Answers1

2
  1. Do not update the GUI from your implementation of doInBackground().

  2. Do update the GUI from your implementation of process() or done(), as shown here, and here.

  3. You may have to re-factor your checkNDownloadFile() method to provide the desired granularity for a sensible progress display.

  4. See also Watching a Directory for Changes.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • If i were to put the checkNDownloadFile.start() into done() wouldn't that defeat the point? – TrinaryAtom Apr 12 '14 at 10:24
  • 1
    Correct; you should `publish()` intermediate results concering the download from `doInBackground()`. – trashgod Apr 12 '14 at 10:26
  • Ah, right on. So is it still ok to create the JFrame in this SwingWorker as well? just as long as I update it using done/publish or another swingworker later. – TrinaryAtom Apr 12 '14 at 10:29
  • the checkNDownloadFile will not have a progress with it. Just four states, Off-Executing-Good-Bad – TrinaryAtom Apr 12 '14 at 10:31
  • 1
    No; create the frame on the [EDT](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html); see also this related [example](http://stackoverflow.com/a/20603012/230513). – trashgod Apr 12 '14 at 10:40