Search code examples
javanetbeansnetbeans-6.9jprogressbar

NetBeans - Need assistance in updating jProgressBar from autogenerated code


(Ignore following sentence - I was being dumb). I have a feeling that I am calling the updates to my jProgressBar on the EDT (which I know is wrong).

@Action
public Task launchScenario() {

    return new LaunchScenarioTask(getApplication());
}

private class LaunchScenarioTask extends org.jdesktop.application.Task<Object, Void> {

    LaunchScenarioTask(org.jdesktop.application.Application app) {
        super(app);
        launchTestCaseButton.setEnabled(false);
        progressBar.setIndeterminate(false);
        statusPanel.validate();
        statusPanel.repaint();
        totalTests = scenarioRepeat * selectedSSIDS.length;
        setProgress(0);
    }

    @Override
    protected Object doInBackground() {
        currentScenario = 1;

        progressBar.setMaximum(totalTests);
        progressBar.setMinimum(0);

        try {
            Thread.sleep((long) 500);
        } catch (InterruptedException ignore) {
        }

        int ssidsToTest = selectedSSIDS.length;

        //For every SSID, we iterate through and create an autoconnector object
        for (int counter = 0; counter < ssidsToTest; counter++) {

            try {
                setMessage("Preparing " + selectedSSIDS[counter] + " test case...");
                Thread.sleep((long) 2000);
            } catch (InterruptedException ignore) {
            }

            setMessage("Launching ");
            AutoConnector ac = new AutoConnector(selectedSSIDS[counter]);

            for (int i = 0; i < scenarioRepeat; i++) {
                if (ac.connected()) {
                    setMessage("Running test " + currentScenario + "/" + totalTests);
                    currentScenario++;
                    passedTests++;
                } else {
                    currentScenario++;
                    failedTests++;
                }

                System.out.println("setting progress");
                setProgress((currentScenario / totalTests) * 100);
                progressBar.setString((currentScenario / totalTests) * 100 + "% complete");
            }

        }
        try {
            Thread.sleep((long) 500);
        } catch (InterruptedException ignore) {
        }
        return null;  // return your result
    }

    @Override
    protected void succeeded(Object result) {
        launchTestCaseButton.setEnabled(true);
        setMessage("Complete. " + passedTests + " tests passed and "
                + failedTests + " tests failed.");
    }
}

I know for a fact (and feel a bit bad) that I'm calling the updates (setProgress() and setMessage() in the doInBackground() method, and I have a sneaking suspicious that I should not be. <<

So the question is, where should I place the setProgress() snippets in the code? Would this require me creating a task with the AutoConnector class and seeing if I can update the jProgressBar from there?

I have used jProgressBars before, but not in this manner with NetBeans.


Solution

  • Your problem is just the opposite of what you state at the beginning of your question. You state:

    I have a feeling that I am calling the updates to my jProgressBar on the EDT (which I know is wrong).

    And just the opposite is true. All updates to the JProgressBar should be made on the EDT, and you're doing just the opposite. You should call no methods on your JProgressBar from within your SwingWorker's doInBackground(...) method.

    Instead consider

    • changing a listened to property such as your SwingWorker's progress property, and have the JProgressBar get updated in a PropertyChangeListener that listens for changes to this property, or
    • Use SwingWorker's publish/process method pair allow you to transmit data from the SwingWorker to your GUI on the EDT.

    Myself, I much prefer adding a PropertyChangeListener to my SwingWorker (on the EDT), and updating my JProgressBar from within this listener. I feel that it allows for better code separation with lower coupling and higher cohesion than the first option.