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
Solved

Jprogress bar

Posted on 2004-03-27
9
339 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
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…
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…
The viewer will learn how to implement Singleton Design Pattern in Java.

790 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