Java Reference
In-Depth Information
FileInputStream fis = new FileInputStream(filename);
ProgressMonitorInputStream pmis =
new ProgressMonitorInputStream(parent, "Reading " + filename, fis);
Note The minimum...maximum range for the ProgressMonitorInputStream ProgressMonitor is
[0... size of stream ].
Using a ProgressMonitorInputStream
As with all input streams, once you've created a ProgressMonitorInputStream , you need to read
from it. If the input stream isn't read quickly enough, the underlying ProgressMonitor causes
the progress pop-up window to appear. Once that window appears, a user can monitor the
progress or cancel the reading by selecting the Cancel button. If the Cancel button is selected,
an InterruptedIOException is thrown, and the bytesTransferred field of the exception is set to
the number of bytes successfully read.
Figure 9-17 shows what one ProgressMonitorInputStream pop-up might look like. For a
little variety, the pop-up uses two JLabel components in the message, instead of just one.
Figure 9-17. ProgressMonitorInputStream pop-up
Listing 9-6 shows a complete source example. The boldfaced lines are the keys to using the
ProgressMonitorInputStream . They set up the dialog box's message and create the input stream.
The program uses a file name specified from the command line, reads the file, and copies the
file to standard output (the console). If the file is large enough, the progress monitor will appear.
If you press the Cancel button, the reading stops and Canceled is printed to standard error.
Listing 9-6. ProgressMonitorInputStream Demonstration
import java.io.*;
import java.awt.*;
import javax.swing.*;
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;
 
Search WWH ::




Custom Search