Java Swing – JFileChooser示例

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来获取用户想要打开的文件的绝对路径或获取用户想要保存文件的位置的示例:

FileChooser1.java
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()相似,区别在于开发人员如何处理每个方法。 出于可读性原因,我不建议将两种方法混合使用。

输出:

swing-jfilechooser-4a

当用户导航到目录时,选择一个文件并单击“打开”

swing-jfilechooser-4b

输出:

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的示例:

FileChooser2.java
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());
			}
		}

	}

}

输出:

swing-jfilechooser-4c
You selected the directory: C:\Users\Public\Pictures

3. setMultiSelectionEnabled(Boolean)– 允许多个选择

启用多选的示例。 用户选择多个文件,程序将打印名称:

FileChooser3.java
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());
				}
			});
		}

	}

}

输出:

swing-jfilechooser-4d
Directories found

Camera Roll
Saved Pictures

- - - - - - - - - - -

Files Found

autumn_rhythm-pollock1.jpg
kuNUfO.jpg
mona.jpg

4.过滤器– 限制显示给用户的文件集

将用户的选择限制在程序的需求上总是很方便的。 例如,如果您的程序需要png和gif图像,则最好将用户的选择限制为仅此。 下面的示例显示如何使用自定义FileNameExtensionFilter实现此目的:

FileChooser4.java
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());
		}

	}

}

输出:

swing-jfilechooser-4e

如您所见,不允许用户选择其他任何东西。 上面显示的目录也包含其他类型的图像,但仅向用户显示gif和png。

该目录如下所示:

swing-jfilechooser-4f

5.使用showDialog()

如果您需要自定义批准按钮,则使用showDialog()方法。 以下是使用方法的示例:

FileChooser5.java
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());
		}

	}

}

输出:

swing-jfilechooser-4g

注意
JFileChooser有一个名为setApproveButtonText(String) 。 此方法的问题在于它仅适用于showOpenDialog() 。 当需要自定义按钮时,建议使用showDialog()代替showSaveDialog()

您还应该检查最简单,最常用的方法来读写文件:

参考文献

  1. JFileChooser – Java 8 API
  2. FileSystemView – Java 8 API
  3. FileNameExtension – Java 8 API

翻译自: https://mkyong.com/swing/java-swing-jfilechooser-example/

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值