📄 filechooser.html
字号:
<p><code>JFileChooser</code> supports three different kinds of filtering.The filters are checked in the order listed here.For example, an application-controlled filter sees onlythose files accepted by the built-in filtering.<dl><dt> <strong>Built-in filtering</strong><dd> Filtering is set up through specific method calls on a file chooser. Currently the only built-in filter available is for hidden files, such as those that begin with period (.) on UNIX systems. By default, hidden files are not shown. Call <code>setFileHidingEnabled(false)</code> to show hidden files.<dt> <strong>Application-controlled filtering</strong><dd> The application determines which files are shown. Create a custom subclass of <a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/filechooser/FileFilter.html"><code>FileFilter</code></a>, instantiate it, and use the instance as an argument to <code>setFileFilter</code>. The file chooser shows only those files that the filter accepts.<dt> <strong>User-choosable filtering</strong><dd> The file chooser GUI provides a list of filters that the user can choose from. When the user chooses a filter, the file chooser shows only those file accepted by that filter. <code>FileChooserDemo2</code> adds a custom file filter to the list of user-choosable filters:<blockquote><pre>fc.addChoosableFileFilter(new ImageFilter());</pre></blockquote> By default, the list of user-choosable filters includes the Accept All filter, which lets the user see all non-hidden files. This example uses the following code to disable the Accept All filter:<blockquote><pre>fc.setAcceptAllFileFilterUsed(false);</pre></blockquote> Our custom file filter is implemented in <a class="SourceLink" target="_blank" href="examples/ImageFilter.java"><code>ImageFilter.java</code></a> and is a subclass of <code>FileFilter</code>. The <code>ImageFilter</code> class implements the <code>getDescription</code> method to return "Just Images" — a string to put in the list of user-choosable filters. <code>ImageFilter</code> also implements the <code>accept</code> method so that it accepts all directories and any file that has a <code>.png</code>, <code>.jpg</code>, <code>.jpeg</code>, <code>.gif</code>, <code>.tif</code>, or <code>.tiff</code> filename extension.<blockquote><pre>public boolean accept(File f) { <strong>if (f.isDirectory()) { return true; }</strong> String extension = Utils.getExtension(f); if (extension != null) { if (extension.equals(Utils.tiff) || extension.equals(Utils.tif) || extension.equals(Utils.gif) || extension.equals(Utils.jpeg) || extension.equals(Utils.jpg) || extension.equals(Utils.png)) { return true; } else { return false; } } return false;}</pre></blockquote>By accepting all directories, this filter allows theuser to navigate around the file system.If the bold lines were omitted from this method,the user would be limited to the directory withwhich the chooser was initialized.<P>The preceding code sample uses the <code>getExtension</code>method and several string constants from<a class="SourceLink" target="_blank" href="examples/Utils.java"><code>Utils.java</code></a>,shown here:<blockquote><pre>public class Utils { public final static String jpeg = "jpeg"; public final static String jpg = "jpg"; public final static String gif = "gif"; public final static String tiff = "tiff"; public final static String tif = "tif"; public final static String png = "png"; /* * Get the extension of a file. */ public static String getExtension(File f) { String ext = null; String s = f.getName(); int i = s.lastIndexOf('.'); if (i > 0 && i < s.length() - 1) { ext = s.substring(i+1).toLowerCase(); } return ext; }}</pre></blockquote></a></dl></blockquote><a name="fileview"><h3>Customizing the File View</h3></a><blockquote>In the Java look and feel, the chooser's list showseach file's name and displaysa small icon that represents whether the file is a true fileor a directory.You can customize this <em>file view</em> by creating a custom subclass of<a class="APILink" target="_blank" href="http://java.sun.com/javase/6/docs/api/javax/swing/filechooser/FileView.html"><code>FileView</code></a> and using an instance of the class as an argument to <code>setFileView</code>.The example uses an instance of a custom class,implemented in<a class="SourceLink" target="_blank" href="examples/ImageFileView.java"><code>ImageFileView.java</code></a>,as the file chooser's file view.<blockquote><pre>fc.setFileView(new ImageFileView());</pre></blockquote><code>ImageFileView</code> shows adifferent icon for each type of imageaccepted by the image filter described previously.<p>The <code>ImageFileView</code> class overrides the five abstract methodsdefined in <code>FileView</code> as follows.<dl><dt> <strong><code>String getTypeDescription(File f)</code></strong><dd> Returns a description of the file type. This is not yet used by any look and feel. Here is <code>ImageFileView</code>'s implementation of this method:<blockquote><pre>public String getTypeDescription(File f) { String extension = Utils.getExtension(f); String type = null; if (extension != null) { if (extension.equals(Utils.jpeg) || extension.equals(Utils.jpg)) { type = "JPEG Image"; } else if (extension.equals(Utils.gif)){ type = "GIF Image"; } else if (extension.equals(Utils.tiff) || extension.equals(Utils.tif)) { type = "TIFF Image"; } else if (extension.equals(Utils.png)){ type = "PNG Image"; } } return type;}</pre></blockquote><dt> <strong><code>Icon getIcon(File f)</code></strong><dd> Returns an icon representing the file or its type. Here is <code>ImageFileView</code>'s implementation of this method:<blockquote><pre>public Icon getIcon(File f) { String extension = Utils.getExtension(f); Icon icon = null; if (extension != null) { if (extension.equals(Utils.jpeg) || extension.equals(Utils.jpg)) { icon = jpgIcon; } else if (extension.equals(Utils.gif)) { icon = gifIcon; } else if (extension.equals(Utils.tiff) || extension.equals(Utils.tif)) { icon = tiffIcon; } else if (extension.equals(Utils.png)) { icon = pngIcon; } } return icon;}</pre></blockquote><dt> <strong><code>String getName(File f)</code></strong><dd> Returns the name of the file. Most implementations of this method should return <code>null</code> to indicate that the look and feel should figure it out. Another common implementation returns <code>f.getName()</code>.<p><dt> <strong><code>String getDescription(File f)</code></strong><dd> Returns a description of the file. This is not yet used by any look and feel. The intent is to describe individual files more specifically. A common implementation of this method returns <code>null</code> to indicate that the look and feel should figure it out.<p><dt> <strong><code>Boolean isTraversable(File f)</code></strong><dd> Returns whether a directory is traversable. Most implementations of this method should return <code>null</code> to indicate that the look and feel should figure it out. Some applications might want to prevent users from descending into a certain type of directory because it represents a compound document. The <code>isTraversable</code> method should never return <code>true</code> for a non-directory.</dl></blockquote><a name="accessory"><h3>Providing an Accessory Component</h3></a><blockquote>The customized file chooser in<code>FileChooserDemo2</code>has an accessory component.If the currently selected item is aPNG, JPEG, TIFF, or GIF image,the accessory componentdisplays a thumbnail sketch of the image.Otherwise, the accessory component is empty.Aside from a previewer,probably the most common use for the accessory componentis a panel with more controls on it —say, checkboxes that toggle some features.<p>The example calls the <code>setAccessory</code>method to establish an instance of the <code>ImagePreview</code> class,implemented in<a class="SourceLink" target="_blank" href="examples/ImagePreview.java"><code>ImagePreview.java</code></a>,as the chooser's accessory component:<blockquote><pre>fc.setAccessory(new ImagePreview(fc));</pre></blockquote>Any object that inherits from <code>JComponent</code>can be an accessory component.The component should have a preferred size that looks good in the file chooser.<p>The file chooser fires a property change event when theuser selects an item in the list.A program withan accessory component must register to receive these eventsto update the accessory component whenever the selection changes.In the example, the <code>ImagePreview</code> object itselfregisters for these events.This keeps all the code related to the accessory componenttogether in one class.<p>Here is the example's implementation of the<code>propertyChange</code> method, which isthe method called when a property change event is fired:<blockquote><pre>//<em>where member variables are declared</em>File file = null;...public void propertyChange(PropertyChangeEvent e) { boolean update = false; String prop = e.getPropertyName(); //If the directory changed, don't show an image. if (JFileChooser.DIRECTORY_CHANGED_PROPERTY.equals(prop)) { file = null; update = true; //If a file became selected, find out which one. } else if (JFileChooser.SELECTED_FILE_CHANGED_PROPERTY.equals(prop)) { file = (File) e.getNewValue(); update = true; } //Update the preview accordingly. if (update) { thumbnail = null; if (isShowing()) { loadImage(); repaint(); } }}</pre></blockquote>If <code>SELECTED_FILE_CHANGED_PROPERTY</code> is the propertythat changed,this method gets a <code>File</code> object from the file chooser.The <code>loadImage</code> and <code>repaint</code> methodsuse the <code>File</code> to load the imageand repaint the accessory component.</blockquote><h3><a name="api">The File Chooser API</a></h3><blockquote>The API for using file choosers falls into these categories:<ul><li><a href="#show">Creating and Showing the File Chooser</a><li><a href="#selection">Selecting Files and Directories</a>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -