I am trying to update a JProgressBar in a for loop , The maximum size , I have set is equal to the size of the array which is computed upon and I am incrementing the value of the progress bar in the for loop.Here 's the code

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                       

        for (int i = 0; i < area1.length; i++) {

            if (stopword.contains(area1[i]) || stopword.contains(area1[i].toLowerCase()) || area1[i].equals(" ") || area1[i].length() < 3) {
            } else {

                jTextArea2.append(area1[i] + " ");
                area3 = jTextArea2.getText();


            int x = jProgressBar2.getValue();
            int z = (int) (jProgressBar2.getPercentComplete() * 100);
            jProgressBar2.setValue(x + 1);

But the problem is the progress bar goes from 1 to 100 and doesn't show any processing , Moreover for long text (Larger array size) , the view freezes , GUI doesn't get repainted and the processing goes on , and when processing is over , then only the text is appended into the text area and Jprogress bar goes from 1 to 100 , I have also used delay and threads but with the same problem , What would i need to do to show the progress bar go slowly .

6 Years
Discussion Span
Last Post by ashishnandwani

You should use the Thread.sleep() method in your code to make the progress Bar go slowly.

extend your class with Thread class and write all the code in run() method and then call the


then you can make the progress bar go slowly....


Google "Event Dispatch Thread" (EDT or "Swing" thread).
All GUI updates are single threaded on the EDT, as are all GUI-initiated user interactions. So your jButton2ActionPerformed executes on the EDT. While it's executing all other GUI-related activities are queued (including your progress bar re-paints).
There are a number of ways round this but they all involve moving your long-running task off the EDT onto some other thread, thus un-blocking GUI activity. Generally, the SwingWorker class (new in Java 1.6) is a very good option. The standard JavaDoc includes an example of updating a progress bar from a long-running process.

ps Using Thread sleep inside an ActionPerformed is a common beginner's mistake (as explained above) - it can only help if the process has been handed off to another thread. eg by using SwingWorker. In that case you can use sleep to artificially slow down your progess, if that's what you want.

Edited by JamesCherrill: ps Re sleep


I did use SwingWorker inside the Action performed method and overrided the background method to update the Jprogressbar but the code did not run , Anything i am doing wrong?


If it didn't run then, yes, you're doing something wrong!
But without (a) the code and (b) an EXACT description of what does or does not happen when you try to run it (including the full stack trace from any Exceptions), there's nothing more anyone here can do...


Most of How to Use SWingWorker are only with JProgressBar, better examples and tutorial descibe that with implements PropertyChangeListener, this is correct and safiest way

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.