28.12.2012 Views

Core JFC Java Foundation Classes 2nd edition - Read

Core JFC Java Foundation Classes 2nd edition - Read

Core JFC Java Foundation Classes 2nd edition - Read

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

992 Chapter 8 Creating Custom Dialogs<br />

cally abort the operation that is being monitored for any reason, you must call<br />

the progress monitor's close method to allow it to dismiss the dialog.<br />

It is possible to demonstrate ProgressMonitor by making some changes<br />

to the actionPerf ormed method of the previous example. Instead of having<br />

ProgressMonitorlnputStream create and manage the Progress-<br />

Monitor behind the scenes, the new version will create its own and have it<br />

display not only the current progress but also how long it thinks the operation<br />

will take to complete. To do this, the setNote method will be used to write a<br />

text string with the current time remaining onto the dialog box. Listing 8-17<br />

shows the new actionPerf ormed method.<br />

<strong>Core</strong> Note<br />

If you press the Cancel button on the dialog shown by<br />

ProgressMonitorlnputStream, the I/O operation is terminated with<br />

an exception because the ProgressMonitorlnputStream is handling<br />

the 110. If you use a ProgressMonitor, dismissing it has no effect on<br />

the I/O, because it isn't directly involved in it.<br />

Listing 8-17 Displaying extra information with a<br />

ProgressMonitor<br />

public void actionPerformed(ActionEvent evt) {<br />

wordsFound.setText("");<br />

fileName.setEnabled(false);<br />

final String name = fileName.getText().trim();<br />

final File f = new File(name);<br />

final float size = (float)new File(name).length{)<br />

II Total size of file<br />

monitor = new ProgressMonitor(this,<br />

"Counting words in " + name,<br />

TIME_STRING,<br />

0, (int)size) ;<br />

Thread t = new Thread() {<br />

public void run() {<br />

Buffered<strong>Read</strong>er r = null;<br />

try {<br />

words = 0;

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!