Solved

Jprogress bar

Posted on 2004-03-27
9
360 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
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…
This video teaches viewers about errors in exception handling.
Suggested Courses

615 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