在指定目录启动JFileChooser并仅显示特定类型的文件 [英] Starting a JFileChooser at a specified directory and only showing files of a specific type
问题描述
我有一个使用JFileChooser的程序。简而言之,完整的程序是一个GUI,允许用户操纵PNG和JPG。我想这样做,以便JFileChooser立即打开图片目录(窗口)。当用户打开他们的JFileChooser时,它会直接打开图片库C:\ Users \(USER)\ Pictures
I have a program utilizing a JFileChooser. To be brief, the full program is a GUI which allows users to manipulate PNGs and JPGs. I would like to make it so that the JFileChooser instantly opens to the picture directory (windows). When the user opens their JFileChooser, it would open directly to the pictures library C:\Users\(USER)\Pictures
此外,它会很好仅显示特定类型的文件(PNG和JPG)。许多程序似乎能够做到这一点;只允许选择特定文件。 JFileChooser是否允许这样的事情?目前,我正在使用一种大规模不可靠的运行方法来拒绝非PNG / JPG。
Furthermore, it would be nice to ONLY show files of a specific type (PNGs and JPGs). Many programs seem to be able to do this; only allowing selection of specific files. Does JFileChooser allow such a thing? Currently, I am using a massively unreliable, run around method to reject non-PNGs/JPGs.
以下是指GUI的浏览按钮,其中用户将选择他们的图片进行编辑,它将在屏幕上显示。
The following refers to the "browse" button of the GUI, in which a user will select their picture for editing and it will display it on the screen.
try {
int val = filec.showOpenDialog(GridCreator.this);
if(val==JFileChooser.APPROVE_OPTION) {
File unfiltered_picture = filec.getSelectedFile();
//get the extension of the file
extension=unfiltered_picture.getPath();
int index=extension.indexOf(".");
extension=extension.substring(index+1, extension.length());
//if the file is not jpg, png, or jpeg, reject it and send a message to the user.
if(!extension.matches("[jJ][pP][gG]") && !extension.matches("[pP][nN][gG]") && !extension.matches("[jJ][pP][eE][gG]")) {
JOptionPane.showMessageDialog(null,
"cannot load file. File must be of type png, jpeg, or jpg. \n Your file is of type " + extension,
"Error: improper file",
JOptionPane.OK_OPTION);
//if the file is of the proper type, display it to the user on the img JLabel.
} else {
finalImage = ImageIO.read(unfiltered_picture);
ImageIcon imgIcon = new ImageIcon();
imgIcon.setImage(finalImage);
img.setIcon(imgIcon);
img.invalidate();
h_divide.setValue(0);
v_divide.setValue(0);
}
}
} catch(IOException exception) {
exception.printStackTrace();
}
谢谢。
推荐答案
您需要使用要启动的目录构建 JFileChooser
,然后传递 FileFilter设置可见之前
进入它。
You need to construct your JFileChooser
with the directory you want to start in and then pass a FileFilter
into it before setting visible.
final JFileChooser fileChooser = new JFileChooser(new File("File to start in"));
fileChooser.setFileFilter(new FileFilter() {
@Override
public boolean accept(File f) {
if (f.isDirectory()) {
return true;
}
final String name = f.getName();
return name.endsWith(".png") || name.endsWith(".jpg");
}
@Override
public String getDescription() {
return "*.png,*.jpg";
}
});
fileChooser.showOpenDialog(GridCreator.this);
此示例过滤以.png或.jpg结尾的文件。
This example filters for files ending in ".png" or ".jpg".
这篇关于在指定目录启动JFileChooser并仅显示特定类型的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!