JFileChooser inherits the JComponent. JComponent is also a base-level component of the swing library. Syntax: Below given declaration below shows how a JFileChooser is used in the program. JFileChooser class provides a certain parameter to set the attribute for the file selection dialog.
This dialog opens at the default path. Users can select files or folders on the default path. Users can select a file or folder on the specified path as given in the constructor parameter. JFileChooser with the File parameter as in constructor will prompt a dialog with the specified file directory path. JFileChooser constructor with the parameter as an object in the FileSystemView prompt a dialog in the specified file-system view.
String setDirectory public void setDirectory String dir Sets the directory of this file dialog window to be the specified directory. Specifying a null or an invalid directory implies an implementation-defined default. This default will not be realized, however, until the user has selected a file. Until this point, getDirectory will return the value passed into this method. Specifying "" as the directory is exactly equivalent to specifying null as the directory.
Parameters: dir - the specified directory See Also: getDirectory getFile public String getFile Gets the selected file of this file dialog. Returns: the currently selected file of this file dialog window, or null if none is selected See Also: setFile java. String getFiles public File [] getFiles Returns files that the user selects. If the user cancels the file dialog, then the method returns an empty array.
Returns: files that the user selects or an empty array if the user cancels the file dialog. Since: 1. This file becomes the default file if it is set before the file dialog window is first shown. When the dialog is shown, the specified file is selected. The kind of selection depends on the file existence, the dialog type, and the native platform.
This method accepts either a full file path, or a file name with an extension if used together with the setDirectory method. Specifying "" as the file is exactly equivalent to specifying null as the file. Parameters: file - the file being set See Also: getFile , getFiles setMultipleMode public void setMultipleMode boolean enable Enables or disables multiple file selection for the file dialog. Parameters: enable - if true , multiple file selection is enabled; false - disabled. Returns: true if the file dialog allows the multiple file selection; false otherwise.
A filename filter allows the user to specify which files appear in the file dialog window. Filename filters do not function in Sun's reference implementation for Microsoft Windows. 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.
Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The Overflow Blog. Stack Gives Back Safety in numbers: crowdsourcing data on nefarious IP addresses. Featured on Meta. New post summary designs on greatest hits now, everywhere else eventually. Linked Related Hot Network Questions. Question feed. Stack Overflow works best with JavaScript enabled.
0コメント