14. 77. 1. JFileChooser |
|
A JFileChooser is a dialog to select a file or files. |
The return value of the three methods is one of the following: |
- JFileChooser.CANCEL_OPTION, if the user clicks Cancel.
- JFileChooser.APPROVE_OPTION, if the user click an OK/Open/Save button.
- JFileChooser.ERROR_OPTION, if the user closes the dialog
|
A return value of JFileChooser.APPROVE_OPTION, indicates that you can call its getSelectedFile or
getSelectedFiles methods: |
public java.io.File getSelectedFile ()
public java.io.File[] getSelectedFiles ()
|
|
JFileChooser has supporting classes: FileFilter class, FileSystemView class, FileView. |
FileFilter class is for restricting files and directories to be listed in the FileView of the JFileChooser.
The FileView controls how the directories and files are listed within the JFileChooser.
The FileSystemView is an abstract class that tries to hide file system-related operating system
specifics from the file chooser. |
import javax.swing.JFileChooser;
import javax.swing.JFrame;
public class MainClass extends JFrame {
public MainClass() {
JFileChooser fileChooser = new JFileChooser();
fileChooser.setDialogTitle("Choose a file");
this.getContentPane().add(fileChooser);
fileChooser.setVisible(true);
}
public static void main(String[] args) {
JFrame frame = new MainClass();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}
|
|