2

QUESTION SOLVED!!!!!! Many thanks to trashgod and HoverCraftFullOfEels! I finally got the concept by using the below example and altering it slightly. The alteration allows scaling the progress bar (by default is 100 units). Again, THANK YOU for your patience and willingness to work through this. Means a lot guys, ~Kyte

ps - +1's all 'round :)

/** @see http://stackoverflow.com/questions/4637215 */
public class Threading_01 extends JFrame {

private static final String s = "0.00";
private JProgressBar progressBar = new JProgressBar(0, 100);
private JLabel label = new JLabel(s, JLabel.CENTER);

public Threading_01() {
    this.setLayout(new GridLayout(0, 1));
    this.setTitle("√2");
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.add(progressBar);
    this.add(label);
    this.setSize(161, 100);
    this.setLocationRelativeTo(null);
    this.setVisible(true);
}

public void runCalc() {
//    progressBar.setIndeterminate(true);
    TwoWorker task = new TwoWorker();
    task.addPropertyChangeListener(new PropertyChangeListener() {

        @Override
        public void propertyChange(PropertyChangeEvent e) {
            if ("progress".equals(e.getPropertyName())) {
                progressBar.setIndeterminate(false);
                progressBar.setValue((Integer) e.getNewValue());
                System.out.println("**: " + e.getNewValue());
            }
        }
    });
    task.execute();
}

private class TwoWorker extends SwingWorker<Double, Double> {

    private static final int N = 734;
    private final DecimalFormat df = new DecimalFormat(s);

    @Override
    protected Double doInBackground() throws Exception {
        int cntr = 1; //
        double d1;
        double d2;
        double d3;
        for (int i = 0; i <=N; i++) {   
            d1 = N;
            d2 = i;
            d3 = d2/d1;
            d3 = d3 * 100;
            System.out.println(i + "++ " + d3);
            if(d3 >= cntr){
                System.out.println(i + "UPDATE");
                cntr++;
                setProgress(cntr);
                publish(Double.valueOf(cntr));
                Thread.sleep(250); // simulate latency
            }
        }
        return Double.valueOf(0);
    }

    @Override
    protected void process(List<Double> chunks) {
        for (double d : chunks) {
            label.setText(df.format(d));
            System.out.println(": " + d);
        }
    }
}

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {

        @Override
        public void run() {
            Threading_01 t = new Threading_01();
            t.runCalc();
        }
    });
}

}

Kyte
  • 834
  • 2
  • 12
  • 27
  • 5
    Can you post either your current attempt at a simple compilable and runnable version of what you're trying to do complete with your attempt to use a SwingWorker, best as an [sscce](http://sscce.org) if possible? Either that or go into significant detail on exactly where you're stuck, since all I'm getting from the above is that you're completely stuck on everything. If that's the case, then you'll need to re-review the tutorials. But either way, please let us know! – Hovercraft Full Of Eels Jul 21 '12 at 17:06
  • 2
    Other links to examples (mine!) include [this one](http://stackoverflow.com/a/5533581/522444) and [this other one](http://stackoverflow.com/a/10240173/522444). – Hovercraft Full Of Eels Jul 21 '12 at 17:13
  • 1
    @HFoE's [example](http://stackoverflow.com/a/5533581/522444) illustrates how the worker can expand it's use of the [_observer pattern_](http://stackoverflow.com/a/5533581/522444); +1 on both. You may want to adapt one of his or my cited [examples](http://stackoverflow.com/a/11594200/230513) to construct your [sscce](http://sscce.org/). – trashgod Jul 21 '12 at 17:26
  • I'd 1+ trashgod's examples, but I've already done so long ago. :( Both are excellent. – Hovercraft Full Of Eels Jul 21 '12 at 17:34
  • I'll try to update the code with my attempt at a swing worker. I'm thinking about redesigning it slightly by creating a swing worker that will handle I/O when an email is written to disk. If the class is say, somewhere in the order of 376-ish lines of code, do you still want it? (yes, I'll trim the unnecessary parts in compliance with sscce :) – Kyte Jul 21 '12 at 23:12
  • @Kyte: That's ~3-5x the size needed. The goal is not to satisfy an arbitrary limit, but to focus on the problem, e.g. progress. Substitute `Thread.sleep()` for `readOneUnit()` to get started. Use a number that approximates your anticipated SMTP latency. Don't forget: we don't have (or want) access to your infrastructure. – trashgod Jul 22 '12 at 01:02
  • @trashgod Sorry about that, I wasn't sure what to include or exclude. I'm confused about a few things, where am I substituting the sleep for a readOneUnit()? Is latency my network latency (<1sec) or the time required for I/O writing to disk? – Kyte Jul 22 '12 at 01:44
  • @Kyte: simulate latency in `doInBackground()`. Focus on a single aspect: e.g. reading or writing. A constant is fine; in contrast, this [example](http://stackoverflow.com/a/11372932/230513) uses `rand.nextInt(42) + 10`. Don't try to learn/do everything all at once. :-) – trashgod Jul 22 '12 at 02:15
  • @trashgod Alright, I'm still having a rough time understanding this. The doInBackground is where I should do the disk I/O operations (for now, simulated by a sleep() statement). Which means that the done() method is where the updates should go for the progress bar. Am I starting to get this? ps- *thank you* for your patience – Kyte Jul 22 '12 at 18:17
  • I can't follow your example: It doesn't compile, it includes considerable extraneous material, and it has no `PropertyChangeListener`, which is how `setProgress()` moves the bar. – trashgod Jul 22 '12 at 19:51
  • I know. I think I'm going to do a LOT of research and throw the project out and start over. I'm trying to teach myself threading :P – Kyte Jul 22 '12 at 19:55

1 Answers1

5

Updating a progress bar is usually handled in a PropertyChangeListener, as shown here. See also this related example. Invoking setIndeterminate(true) is a reasonable initial setting if your worker can later offer more definitive progress.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045