2

It is the first time I have to work with a progress bar and I'm facing a problem, besides I try to call its setValue(x) from everywhere it keeps on 0% and goes straight to 100% after my method routine finishes.

I tried to make an inner class that extends Thread, then after I tried to start a new Thread within my "main" method, then for the last I tried to use the Observer. These ones seems to have worked according to this posts but unfortunately not to me

Update JProgressBar from new Thread

Problem making a JProgressBar update values in Loop (Threaded)

please, could someone help me???

public class MainClass {      

private void checkFiles() {

    Task task = new Task();
    task.start();

    //here I have some Files validation...I don't think it is important to solve the progressbar problem 
    //so it will be ommited


    //in this point I tried to call update to test the observer solution I found in another post here
    //task.update(null, null);

    JOptionPane.showMessageDialog(this, "Done!");
    //here the bar jumps from 0% to 100%

    }


    private class Task extends Thread implements Observer {

    public Task() {
    }

    //Dont bother with the calculum as I haven't finished working on them....
    //The relevant thing here is that it starts a new Thread and I can see the progress
    //increasing on console using system.out but my progress bar still don't change from 0%.
    public void run() {
        int maxSize = 100;
        final int partsSize = maxSize / listaArquivosSelecionados.size();
        while (listFilesValidated.size() != listFilesToValidate.size()) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
            int progress = listFilesValidated.size() * partsSize;
            System.out.println("Progress" + progress);
            progressBar.setValue(progress);

            }
        });
        try {
            Thread.sleep(100);
        }
        catch (InterruptedException e) {}
        }
    }

    //Just tried to set any value to check if it would update before the files validation thread finishes its work.
    @Override
    public void update(Observable arg0, Object arg1) {
        progressBar.setValue(66);
    }
}
Community
  • 1
  • 1
Scrougge
  • 151
  • 2
  • 17
  • Your problem is one of threading -- you're either calling long-running code on the Swing event thread or you're trying to change the progress bar's properties off of the Swing event thread. Likely it's the first and not the second, but either way it must be fixed. You must look through your code and make sure that you're handling Swing threading correctly. Look up "Concurrency in Swing" and study that series of articles for more details as well as the JProgressBar tutorial. – Hovercraft Full Of Eels Feb 12 '16 at 12:54
  • 1
    For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson Feb 12 '16 at 13:26

1 Answers1

4

You can create another class of ProgressBar (see Oracle tutorial) and use this:

ProgressBar pbFrame = new ProgressBar();
pbFrame.setVisible(true);       
Executors.newSingleThreadExecutor().execute(new Runnable() {
        @Override
        public void run() {
            // run background process

        }
    });

Or you can use SwingWorker, for example:

SwingWorker worker = new SwingWorker<MyReturnType, Void>() {
    @Override
    public MyReturnType doInBackground() {
        // do your calculation and return the result. Change MyReturnType to whatever you need
    }
    @Override
    public void done() {
        // do stuff you want to do after calculation is done
    }
};

I had the same question some years ago.

Community
  • 1
  • 1
Denis
  • 503
  • 8
  • 32
  • 1
    For reference, you can call `setProgress()` from the `doInBackground()` method of `SwingWorker`, for [example](http://stackoverflow.com/a/4637725/230513). I'm less sanguine about your first approach; more [here](http://stackoverflow.com/a/33710937/230513). – trashgod Feb 12 '16 at 13:31