Solved

Jprogress bar

Posted on 2004-03-27
9
353 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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

Suggested Solutions

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
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…
Suggested Courses

751 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