With this method, we can limit the user to select either directories only JFileChooser. Here is an example that implements JFileChooser. The example below shows how to achieve this using the custom FileNameExtensionFilter class:. Output: As you can see, the user is not allowed to choose anything else. The directory shown above also contains other types of images, but only jpg and gif are displayed to the user.
The filters are checked in the order listed here. For example, an application-controlled filter sees only those files accepted by the built-in filtering. The preceding code sample uses the getExtension method and several string constants from Utils. In the Java look and feel, the chooser's list shows each file's name and displays a small icon that represents whether the file is a true file or a directory.
You can customize this file view by creating a custom subclass of FileView and using an instance of the class as an argument to the setFileView method. The example uses an instance of a custom class, implemented in ImageFileView. The ImageFileView class shows a different icon for each type of image accepted by the image filter described previously.
The customized file chooser in FileChooserDemo2 has an accessory component. Otherwise, the accessory component is empty. Aside from a previewer, probably the most common use for the accessory component is a panel with more controls on it such as check boxes that toggle between features. The example calls the setAccessory method to establish an instance of the ImagePreview class, implemented in ImagePreview.
Any object that inherits from the JComponent class can be an accessory component. The component should have a preferred size that looks good in the file chooser. The file chooser fires a property change event when the user selects an item in the list. A program with an accessory component must register to receive these events to update the accessory component whenever the selection changes. In the example, the ImagePreview object itself registers for these events.
This keeps all the code related to the accessory component together in one class. Here is the example's implementation of the propertyChange method, which is the method called when a property change event is fired:. The loadImage and repaint methods use the File object to load the image and repaint the accessory component. This table shows the examples that use file choosers and points to where those examples are described.
All rights reserved. Hide TOC. Using Swing Components. Try this: Compile and run the example, consult the example index. Click the Open a File button. Navigate around the file chooser, choose a file, and click the dialog's Open button. Use the Save a File button to bring up a save dialog. Try to use all of the controls on the file chooser. In the source file FileChooserDemo. Then compile and run the example again. You will only be able to see and select directories, not ordinary files.
Creates a file chooser instance. The File and String arguments, when present, provide the initial directory. Shows a modal dialog containing the file chooser. Related Articles. Table of Contents. Improve Article. Save Article. Like Article. Last Updated : 14 Apr, Attention reader! Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Next Different ways of Reading a text file in Java. Recommended Articles.
0コメント