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) {                                       
        jProgressBar2.setValue(0);

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

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


                jProgressBar2.setBorderPainted(true);
                System.out.println(area1[i]);
                jTextArea2.append(area1[i] + " ");
                jTextArea2.setCaretPosition(jTextArea2.getDocument().getLength());
                area3 = jTextArea2.getText();


            }

            int x = jProgressBar2.getValue();
            int z = (int) (jProgressBar2.getPercentComplete() * 100);
            System.out.println(z);
jProgressBar2.setAlignmentX(JProgressBar.TOP+100);
            jProgressBar2.repaint();
            jProgressBar2.setValue(x + 1);
            jTextArea2.repaint();
            repaint();
        }
        System.out.println(jProgressBar2.getValue());
    }

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 .

Recommended Answers

All 6 Replies

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

sleep()method.

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.

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

I am really sorry , It was a very silly mistake , I forgot to call execute();

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.