Question

I have a main class that has a JProgressBar. From the main class I create a instance of a class (named Opener) that has a method that reads text files that are usually big (>50000 lines of text).

I pass a reference of the JProgressBar to the method of the Opener class that reads the txt files, so that progressbar is accessible from the Opener class.

The method is defined like this:

 public void readtxtfile (JProgressBar myjProgressBar){....}

I call the method like this:

 Opener myopener = new Opener();
 myopener.readtxtfile (jProgressBar1);

I know that the reference to the progressbar is working correctly because I can set it visible from there (when the main class is created the progressbar is set not visible).

In the readtxtfile method I change the value of the progressbar as the txt file is read.

 myjProgressBar.setValue(mycounter);
 myjProgressBar.revalidate();
 myjProgressBar.repaint();

My problem is that the progress bar is not repainted as its value changes. What should I do?

P.S: When the all reading of the txt file is done, the progress bar at last refreshes. But mean while nothing visible happens to it.

Was it helpful?

Solution

It sounds more like you're trying to read the file from within the Event Dispatching Thread, preventing it from performing any repaints.

Probably the best choice would be to use a SwingWorker to run in the background and load the contents of the file and use it's property change support to update the progres.

Take a look at Worker Threads and SwingWorker for details

Take a look at this and this for examples

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top