23.07.2013 Views

Java IO.pdf - Nguyen Dang Binh

Java IO.pdf - Nguyen Dang Binh

Java IO.pdf - Nguyen Dang Binh

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Figure 13.4. The choosable file filters pop-up in a file chooser<br />

<strong>Java</strong> I/O<br />

Annoyingly, these file filters are not instances of the java.io.FileFilter interface you're<br />

already familiar with. Instead, they're instances of a new abstract class in the<br />

javax.swing.filechooser package. Because of name conflicts with the<br />

java.io.FileFilter interface, any file that imports both packages will have to use the fully<br />

qualified name.<br />

public abstract class javax.swing.filechooser.FileFilter<br />

I would not be surprised to see these replaced by a<br />

java.io.FileFilter objects in a future release.<br />

javax.swing.filechooser.FileFilter is an abstract class instead of<br />

an interface, and it declares a getDescription() method as well as an<br />

accept() method; but other than these minor differences, the two<br />

versions of FileFilter are identical in all important respects. Another<br />

possibility is that the javax.swing.filechooser.FileFilter class<br />

will be renamed javax.swing.filechooser.ChoosableFileFilter<br />

to avoid the annoying name conflict.<br />

This class declares two methods, both abstract:<br />

public abstract boolean accept(File f);<br />

public abstract String getDescription();<br />

The accept() method returns true if the file passes the filter and should be displayed in the<br />

chooser, false if it shouldn't be. Unlike the accept() method java.io.FilenameFilter,<br />

this accept() method is called to filter directories as well as files. Most filters will accept all<br />

directories to allow the user to navigate between directories. The getDescription() method<br />

returns a string describing the filter to be shown to the user in the chooser's pop-up menu; for<br />

example, Text files (*.txt, *.text). Example 13.6 is a simple file filter that only passes<br />

<strong>Java</strong> source code files:<br />

319

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

Saved successfully!

Ooh no, something went wrong!