Solved

Jprogress bar

Posted on 2004-03-27
9
305 Views
Last Modified: 2008-02-01
I have a Jprogress bar...I am trying to learn how to use it... this is the method i created to play around with it:

public void test() {
        importProgress.setMaximum(200000);
        for(int x=0;x<200000;x++){
            System.out.println(x);
            importProgress.setValue(x);
            importProgress.setStringPainted(true);
           
        }
    }

When I call the method, the loop does its proccessing, but the progress bar does not update until the loop is done. I want it to update in real time.. I assume i  need a listener for it. if this true what would the listener look like
thanks in advance
0
Comment
Question by:91mustang
  • 5
  • 4
9 Comments
 
LVL 30

Expert Comment

by:GrandSchtroumpf
Comment Utility
you need a separate thread:

  public void test() {
   
    Thread progressThread = new Thread() {
      public void run() {
        importProgress.setMaximum(200000);
        for(int x=0;x<200000;x++){
          System.out.println(x);
          importProgress.setValue(x);
          importProgress.setStringPainted(true);
        }
      }
    };
    progressThread.start();
   
  }
0
 
LVL 30

Expert Comment

by:GrandSchtroumpf
Comment Utility
setStringPainted(true) can be called outside the loop.
and you can use the sleep() inside your loop:

  public void test() {
    Thread progressThread = new Thread() {
      public void run() {
        importProgress.setMaximum(100);
        importProgress.setStringPainted(true);
        for(int x=0;x<=100;x++){
          System.out.println(x);
          importProgress.setValue(x);
          importProgress.repaint();
          try { sleep(100); }
          catch (Exception e) { ; }
        }
      }
    };
    progressThread.start();
  }
0
 
LVL 4

Author Comment

by:91mustang
Comment Utility
Perfect  GrandSchtroumpf, thank you

quick question, this method is going to used i a app that is importing text file to database, usually between 4-5 mill recs per text file(not alot of proccessing per line, but lots of lines.) how much resources will that new thread chew?, and i'm geussing that sleep(100) is pausing the thread for 100 milliseconds. If so what do you think would a reasonabnle sleep time for that thread

thank you again
0
 
LVL 30

Expert Comment

by:GrandSchtroumpf
Comment Utility
i included the thread.sleep() just for the example.
you need to replace that with your import.

      public void run() {
        int numberOfRecords = 1000; // replace that with the actual number
        importProgress.setMaximum(numberOfRecords-1);
        importProgress.setStringPainted(true);
        for(int x=0;x<numberOfRecords;x++){
          importRecord(x);
          importProgress.setValue(x);
        }
      }
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 4

Author Comment

by:91mustang
Comment Utility
GrandSchtroumpf
Sorry for my ignorance.

when i use:

 public void test() {
        Thread progressThread = new Thread() {
            public void run() {
                importProgress.setMaximum(intLineCount-1);
                importProgress.setStringPainted(true);
                for(int x=0;x<=intLineCount;x++){
                   
                    importProgress.setValue(currentLineCount);
                    importProgress.repaint();
                   
                   
                }
            }
        };
        progressThread.start();
    }

the progress bar does nothing until the proccessing is done- and then shows 0% done.

currentLineCount is getting increased in a file read loop(every line)

what am i doing wrong?
0
 
LVL 4

Author Comment

by:91mustang
Comment Utility
>>the progress bar does nothing until the proccessing is >>done- and then shows 0% done.

sorry, actually it says 100% but does not show any values in between 0 and 100
0
 
LVL 30

Accepted Solution

by:
GrandSchtroumpf earned 25 total points
Comment Utility
> does not show any values in between 0 and 100.
that's because the thread that updates the value of the progress bar does not leave enough time to the  swing thread to display the progress.
this is why i added the sleep.  the sleep leaves time to the swing thread to display the progress.
if you do something in the thread that has some kind of delay involved, such as reading the file and processing your database insert, you will see the progress.
look at the comment i posted in the other thread:
http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_20935198.html
0
 
LVL 4

Author Comment

by:91mustang
Comment Utility
thanks again
0
 
LVL 30

Expert Comment

by:GrandSchtroumpf
Comment Utility
you're welcome mustang
:°)
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
factorial example challenge 10 61
count11 challenge 6 47
What is JNDI datasource in spring 1 36
JList custom Cell Renderer refresh 15 39
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now