JFileChooser
是提示用户选择文件或文件保存位置的快速简便的方法。 下面是一些有关如何使用此类的简单示例。
JFileChooser
有6个构造函数:
-
JFileChooser()
–指向用户默认目录的空构造函数 -
JFileChooser(String)
–使用给定的路径 -
JFileChooser(File)
–使用给定的File作为路径 -
JFileChooser(FileSystemView)
–使用给定的FileSystemView -
JFileChooser(String, FileSystemView)
–使用给定的路径和FileSystemView
-
JFileChooser(File, FileSystemView)
–使用给定的当前目录和FileSystemView
调用JFileChooser
构造函数的所有不同方法
//JFileChooser jfc;
//String path = "C:\\Users\\Public";
//File file = new File("C:\\Users\\Public");
//FileSystemView fsv = FileSystemView.getFileSystemView();
//jfc = new JFileChooser();
//jfc = new JFileChooser(path);
//jfc = new JFileChooser(file);
//jfc = new JFileChooser(fsv);
//jfc = new JFileChooser(path, fsv);
//jfc = new JFileChooser(file, fsv);
作者的个人喜好是考虑到FileSystemView
。 在下面的示例中,我们使用FileSystemView.getFileSystemView()
并将其通过getHomeDirectory()
指向主目录。 该过程导致文件类型。 换句话说,我们在考虑FileSystemView
同时使用构造函数JFileChooser(File)
。
1. show * Dialog()– 打开或保存文件
如何使用JFileChooser
来获取用户想要打开的文件的绝对路径或获取用户想要保存文件的位置的示例:
package com.mkyong.jfileChooser;
import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileSystemView;
public class FileChooser1 {
public static void main(String[] args) {
JFileChooser jfc = new JFileChooser(FileSystemView.getFileSystemView().getHomeDirectory());
int returnValue = jfc.showOpenDialog(null);
// int returnValue = jfc.showSaveDialog(null);
if (returnValue == JFileChooser.APPROVE_OPTION) {
File selectedFile = jfc.getSelectedFile();
System.out.println(selectedFile.getAbsolutePath());
}
}
}
注意,两个方法showOpenDialog()
和showSaveDialog()
相似,区别在于开发人员如何处理每个方法。 出于可读性原因,我不建议将两种方法混合使用。
输出:
当用户导航到目录时,选择一个文件并单击“打开”
输出:
C:\Users\Public\Pictures\pollock.she-wolf.jpg
2. setFileSelectionMode(int)– 选择文件或目录
使用此方法,我们可以限制用户选择目录(仅JFileChooser.DIRECTORIES_ONLY
)或仅文件( JFileChooser.FILES_ONLY
)或文件和目录( JFileChooser.FILES_AND_DIRECTORIES
)。 默认值为FILES_ONLY
。 这是实现JFileChooser.DIRECTORIES_ONLY
的示例:
package com.mkyong.jfileChooser;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileSystemView;
public class FileChooser2 {
public static void main(String[] args) {
JFileChooser jfc = new JFileChooser(FileSystemView.getFileSystemView().getHomeDirectory());
jfc.setDialogTitle("Choose a directory to save your file: ");
jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int returnValue = jfc.showSaveDialog(null);
if (returnValue == JFileChooser.APPROVE_OPTION) {
if (jfc.getSelectedFile().isDirectory()) {
System.out.println("You selected the directory: " + jfc.getSelectedFile());
}
}
}
}
输出:
You selected the directory: C:\Users\Public\Pictures
3. setMultiSelectionEnabled(Boolean)– 允许多个选择
启用多选的示例。 用户选择多个文件,程序将打印名称:
package com.mkyong.jfileChooser;
import java.io.File;
import java.util.Arrays;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileSystemView;
public class FileChooser3 {
public static void main(String[] args) {
JFileChooser jfc = new JFileChooser(FileSystemView.getFileSystemView().getHomeDirectory());
jfc.setDialogTitle("Multiple file and directory selection:");
jfc.setMultiSelectionEnabled(true);
jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
int returnValue = jfc.showOpenDialog(null);
if (returnValue == JFileChooser.APPROVE_OPTION) {
File[] files = jfc.getSelectedFiles();
System.out.println("Directories found\n");
Arrays.asList(files).forEach(x -> {
if (x.isDirectory()) {
System.out.println(x.getName());
}
});
System.out.println("\n- - - - - - - - - - -\n");
System.out.println("Files Found\n");
Arrays.asList(files).forEach(x -> {
if (x.isFile()) {
System.out.println(x.getName());
}
});
}
}
}
输出:
Directories found
Camera Roll
Saved Pictures
- - - - - - - - - - -
Files Found
autumn_rhythm-pollock1.jpg
kuNUfO.jpg
mona.jpg
4.过滤器– 限制显示给用户的文件集
将用户的选择限制在程序的需求上总是很方便的。 例如,如果您的程序需要png和gif图像,则最好将用户的选择限制为仅此。 下面的示例显示如何使用自定义FileNameExtensionFilter
实现此目的:
package com.mkyong.jfileChooser;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.filechooser.FileSystemView;
public class FileChooser4 {
public static void main(String[] args) {
JFileChooser jfc = new JFileChooser(FileSystemView.getFileSystemView().getHomeDirectory());
jfc.setDialogTitle("Select an image");
jfc.setAcceptAllFileFilterUsed(false);
FileNameExtensionFilter filter = new FileNameExtensionFilter("PNG and GIF images", "png", "gif");
jfc.addChoosableFileFilter(filter);
int returnValue = jfc.showOpenDialog(null);
if (returnValue == JFileChooser.APPROVE_OPTION) {
System.out.println(jfc.getSelectedFile().getPath());
}
}
}
输出:
如您所见,不允许用户选择其他任何东西。 上面显示的目录也包含其他类型的图像,但仅向用户显示gif和png。
该目录如下所示:
5.使用showDialog()
如果您需要自定义批准按钮,则使用showDialog()
方法。 以下是使用方法的示例:
package com.mkyong.inputDialog;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileSystemView;
public class FileChooser5 {
public static void main(String[] args) {
JFileChooser jfc = new JFileChooser(FileSystemView.getFileSystemView().getHomeDirectory());
jfc.setDialogTitle("Custom button");
int returnValue = jfc.showDialog(null, "A button!");
if (returnValue == JFileChooser.APPROVE_OPTION) {
System.out.println(jfc.getSelectedFile().getPath());
}
}
}
输出:
注意
JFileChooser
有一个名为setApproveButtonText(String)
。 此方法的问题在于它仅适用于showOpenDialog()
。 当需要自定义按钮时,建议使用showDialog()
代替showSaveDialog()
。
您还应该检查最简单,最常用的方法来读写文件:
参考文献
翻译自: https://mkyong.com/swing/java-swing-jfilechooser-example/