The file dialog box is an interface for selecting files. The JFileChooser class in the Javax.swing package can create a file dialog box. Use the constructor JFileChooser() of this class to create an initially invisible modal file dialog box. Then the file dialog box calls the following 2 methods:
showSaveDialog(Componenta);showOpenDialog(Componenta);
Both can make the dialog box visible, but the appearance is different. The showSaveDialog method provides an interface for saving files, and the showOpenDialog method provides an interface for opening files. The parameter a in the above two methods specifies the position of the dialog box when it is visible. When a is null, the file dialog box appears in the center of the screen; if component a is not empty, the file dialog box is displayed centered in front of component a.
The user clicks the "OK", "Cancel" or "Close" icon on the file dialog box, the file dialog box will disappear, and the ShowSaveDialog() or showOpenDialog() method returns one of the following constants:
JFileChooser.APPROVEOPTIONJFileChooser.CANCEL_OPTION
If you want the file types in the file dialog box to be the types required by the user, for example, files with extensions such as .jpeg and other image types, you can use the FileNameExtensionFilter class to create an object in advance. In JDK version 1.6, the FileNameExtensionFilter class is in javax.swing. in the filechooser package.
For example:
FileNameExtensionFilterfilter=newFileNameExtensionFilter(image file, jpg, gif);
Then let the file dialog box call the setFileFilter(FileNameExtensionFilter filter) method to set the file type that the dialog box opens or displays by default to the type specified by the parameter, for example:
chooser.setFileFilter(filter);