-1

I have a JProgressBar progressBar which should update in a for() loop. Actually I already looked at this question: Progress bar not updating during a loop and I tried it with a new Thread, but I don't know why it still doesn't update.

What I tried:

    public void getNewUUID(BufferedWriter output) {
    Menu.progressBar.setMinimum(0);
    Menu.progressBar.setMaximum(100);
    String hashchar = "";
    x = ID_LENGTH/100;
    y=0;

    for(int ch = 0; ch != ID_LENGTH; ch++) {
        done = ch;
        hashchar = "";
        for(int id = 0; id < ID_LENGTH; id++) {
            hashchar = hashchar+ALPHA_CHARS[rnd.nextInt(ALPHA_CHARS.length)];

            try {
                output.write(hashchar);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            hashchar = "";

            new Thread(new Runnable() {
                public void run() {
                    if(done>=x) {
                        x=x+x;
                        y++;
                        Menu.progressBar.setValue(y);

                    }
                }
            }).start();
        }



    }

try {
    output.flush();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
try {
    output.close();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
}
Community
  • 1
  • 1
Freakey
  • 85
  • 1
  • 3
  • 15
  • Probably because you're not using the event dispatch thread for the GUI updates. I'd recommend you to look into SwingWorkers when doing lengthy tasks that requires GUI updates. – aioobe Aug 20 '15 at 10:39
  • When working with swings, use [SwingUtilities](http://docs.oracle.com/javase/7/docs/api/javax/swing/SwingUtilities.html), don't just keep creating new threads. – Codebender Aug 20 '15 at 10:39
  • Could you maybe give me an example? – Freakey Aug 20 '15 at 10:40
  • 1
    possible duplicate of [How to update java GUI from Thread?](http://stackoverflow.com/questions/13543345/how-to-update-java-gui-from-thread) – fabian Aug 20 '15 at 10:40
  • [Swing, how to properly update the UI](http://stackoverflow.com/q/4921009/2991525) – fabian Aug 20 '15 at 10:42
  • Examples of updating a JProgressBar from a background thread: [Example 1](http://stackoverflow.com/a/5533581/522444), [Example 2](http://stackoverflow.com/a/10240173/522444), [Example 3](http://stackoverflow.com/a/13538075/522444), [Example 4](http://stackoverflow.com/a/25694838/522444), [Example 5](http://stackoverflow.com/a/21954632/522444). – Hovercraft Full Of Eels Aug 20 '15 at 11:04

2 Answers2

3

You are performing the progress bar update on a non UI thread. You will need to use SwingUtilities.invokeLater(Runnable r):

        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                if(done>=x) {
                    x=x+x;
                    y++;
                    Menu.progressBar.setValue(y);

                }
            }
        });

This should make sure that the progress bar update takes place on the UI thread, which should cause the progress bar to be refreshed with the new values.

npinti
  • 51,780
  • 5
  • 72
  • 96
  • I feel a bit dumb. Why does it still not update it? I replaced the "new Thread ... " with your code and it still doesnt update it. – Freakey Aug 20 '15 at 10:43
1

As per these similar questions: Example 1, Example 2, Example 3, Example 4, Example 5.

Your best bet is to do the long-running task within a SwingWorker-created background thread, and inside of the worker, set its progress property as your code runs. Then the Swing application can monitor the state of the worker's progress with a PropertyChangeListener, and in the listener set the value of the JProgressBar's progress. Something similar to this perhaps:

public void getNewUUID(BufferedWriter output) {
    // JProgressBar should not be a static field
    Menu.progressBar.setMinimum(0);
    Menu.progressBar.setMaximum(100);
    x = ID_LENGTH / 100;
    y = 0;
    MyWorker myWorker = new MyWorker(output);
    myWorker.addPropertyChangeListener(new MyWorkerListener());
    myWorker.execute();
}

private class MyWorkerListener implements PropertyChangeListener {
    @Override
    public void propertyChange(PropertyChangeEvent evt) {
        if ("progress".equals(evt.getPropertyName())) {
            int progress = (int) evt.getNewValue();

            // TODO: set your JProgressBar's value here  *********
        }

        if (SwingWorker.StateValue.DONE == evt.getNewValue()) {
            MyWorker myWorker = (MyWorker) evt.getSource();
            try {
                myWorker.get();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
        }
    }
}

private class MyWorker extends SwingWorker<Void, Void> {
    private BufferedWriter output;

    public MyWorker(BufferedWriter output) {
        this.output = output;
    }

    @Override
    protected Void doInBackground() throws Exception {
        String hashchar;
        for (int ch = 0; ch != ID_LENGTH; ch++) {
            done = ch;
            hashchar = "";
            for (int id = 0; id < ID_LENGTH; id++) {
                hashchar = hashchar
                        + ALPHA_CHARS[rnd.nextInt(ALPHA_CHARS.length)];
                try {
                    output.write(hashchar);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                hashchar = "";
                if (done >= x) {
                    x = x + x;
                    y++;
                    setProgress(y);
                }
            }
        }
        try {
            output.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            output.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
Community
  • 1
  • 1
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373