/*
Definitive Guide to Swing for Java 2, Second Edition
By John Zukowski
ISBN: 1-893115-78-X
Publisher: APress
*/
import java.awt.Color;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.InterruptedIOException;
import javax.swing.JLabel;
import javax.swing.ProgressMonitorInputStream;
public class ProgressInputSample {
public static final int NORMAL = 0;
public static final int BAD_FILE = 1;
public static final int CANCELED = NORMAL;
public static final int PROBLEM = 2;
public static void main(String args[]) {
int returnValue = NORMAL;
if (args.length != 1) {
System.err.println("Usage:");
System.err.println("java ProgressInput filename");
} else {
try {
FileInputStream fis = new FileInputStream(args[0]);
JLabel filenameLabel = new JLabel(args[0], JLabel.RIGHT);
filenameLabel.setForeground(Color.black);
Object message[] = { "Reading:", filenameLabel };
ProgressMonitorInputStream pmis = new ProgressMonitorInputStream(
null, message, fis);
InputStreamReader isr = new InputStreamReader(pmis);
BufferedReader br = new BufferedReader(isr);
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
try {
Thread.sleep(500);
} catch (Exception e) {
}
}
br.close();
} catch (FileNotFoundException exception) {
System.err.println("Bad File " + exception);
returnValue = BAD_FILE;
} catch (InterruptedIOException exception) {
System.err.println("Canceled");
returnValue = CANCELED;
} catch (IOException exception) {
System.err.println("I/O Exception " + exception);
returnValue = PROBLEM;
}
}
// AWT Thread created - must exit
System.exit(returnValue);
}
}
|