Article View: aus.computers.java
Article #383Re: JProgress Bar
From: Dennis Bulai
Date: Thu, 30 Nov 2000 00:00
Date: Thu, 30 Nov 2000 00:00
98 lines
4661 bytes
4661 bytes
Here's a quick and dirty example of using a progress monitor. All it does
is read in its own source file character by character, maintain a character
count and periodically update the JProgressMonitor. The sleep in the update
thread isn't really necessary to functionality, it just slows things down
enough to see actual operation of the PM
import java.io.*;
import javax.swing.*;
import java.awt.event.*;
public class ProgMon {
public static void main(String[] args){
final JFrame frame = new JFrame("Progress Test");
JButton button = new JButton("Start");
int min =0;
final int max = 25000;
String[] message = new String[2];
message[0] = "Performing operation";
message[1] = "..This will take time..";
final ProgressMonitor monitor = new ProgressMonitor(frame,
message,"Iteration",min,max);
final Runnable runnable = new Runnable() {
public void run(){
int sleepTime = 50;
int counter = 0;
try {
try{
FileInputStream fis = new
FileInputStream("AddUserGUI.java");
int ch;
while ((ch = fis.read()) >= 0){
counter++;
if (counter % 100 == 0){
Thread.sleep(sleepTime);
System.out.println(counter);
monitor.setProgress(counter);
}
}
}
catch( Exception ie){ ie.printStackTrace();
}
System.out.println("loaded "+counter);
// monitor.setNote("Iteration "+counter);
// monitor.setProgress( counter/max);
if (monitor.isCanceled()){
monitor.setProgress( 100 );
// break;
}
//Thread.sleep(sleepTime);
}
catch (Exception dontcare){
}
monitor.close();
}
};
button.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent a){
Thread thread = new Thread(runnable);
thread.start();
}
});
frame.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we){
frame.setVisible(false);
frame.dispose();
System.exit(1);
}
});
frame.getContentPane().add(button);
frame.pack();
frame.setVisible(true);
}
}
DB
Mario Dima wrote in message ...
>I have an application that processes some files, and it I want a visual
>indication of the processing. What better than a JProgress bar. So I
start
>by studying the documentation and all of the books that I have, as well as
>all my web resources, but nowehre can I com up with a stright forward
>example of how to use a JProgress bar. All I want is to give it the total
>number of files to be processed, and tell it to increment every time a file
>is finished, and perhaps a string diplaying the file name.
>
>All help is appreciated.
>
>TIA
>Mario
>
>
Technical Details
Message-ID:
<905v9s$255$1@newpoisson.nosc.mil>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<n7nV5.10645$IE2.568727@news1.sshe1.sk.home.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 111d6b,811890bd19610234 X-Google-Attributes: gid111d6b,public X-Google-Thread: 10d6db,811890bd19610234 X-Google-Attributes: gid10d6db,public X-Google-Thread: 10a146,811890bd19610234 X-Google-Attributes: gid10a146,public X-Google-Thread: 113ae9,811890bd19610234 X-Google-Attributes: gid113ae9,public X-Google-Thread: 10fd6d,811890bd19610234 X-Google-Attributes: gid10fd6d,public From: "Dennis Bulai" <bulai@syzygy-tech.com> Subject: Re: JProgress Bar Date: 2000/11/30 Message-ID: <905v9s$255$1@newpoisson.nosc.mil> X-Deja-AN: 699577190 Distribution: world References: <n7nV5.10645$IE2.568727@news1.sshe1.sk.home.com> X-MimeOLE: Produced By Microsoft MimeOLE V4.72.3612.1700 X-Complaints-To: usenet@newpoisson.nosc.mil X-Trace: newpoisson.nosc.mil 975601788 2213 198.253.149.21 (30 Nov 2000 16:29:48 GMT) Organization: SPAWAR Systems Center, San Diego, CA 92147 NNTP-Posting-Date: 30 Nov 2000 16:29:48 GMT Newsgroups: aus.computers.java,comp.lang.java.gui,comp.lang.java.help,comp.lang.java.misc,comp.lang.java.programmer