Pass JButton to Utility class. Acceptable?

馋奶兔 提交于 2020-01-06 20:01:24

问题


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
    }
}

回答1:


  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.



来源:https://stackoverflow.com/questions/23028825/pass-jbutton-to-utility-class-acceptable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!