Solved

Jprogress bar

Posted on 2004-03-27
9
345 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
ID: 10697810
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
ID: 10697830
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
ID: 10698691
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 30

Expert Comment

by:GrandSchtroumpf
ID: 10698797
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
 
LVL 4

Author Comment

by:91mustang
ID: 10699496
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
ID: 10699585
>>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
ID: 10699751
> 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
ID: 10699837
thanks again
0
 
LVL 30

Expert Comment

by:GrandSchtroumpf
ID: 10699944
you're welcome mustang
:°)
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
difference of if loops 23 68
Error in @AspectJ Based AOP with Spring 2 23
Desingning Refactoring existing code 2 31
import as existing maven project 3 33
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

685 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