问题
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
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.
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();
}
Thank you.
回答1:
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);
This example filters for files ending in ".png" or ".jpg".
回答2:
Read the API: http://docs.oracle.com/javase/6/docs/api/javax/swing/JFileChooser.html
At the very top of the javadoc page is an example of nearly exactly what you want to do:
JFileChooser chooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter(
"JPG & GIF Images", "jpg", "gif");
chooser.setFileFilter(filter);
int returnVal = chooser.showOpenDialog(parent);
if(returnVal == JFileChooser.APPROVE_OPTION) {
System.out.println("You chose to open this file: " +
chooser.getSelectedFile().getName());
}
The class that you are looking for in general is FileFilter
, which is abstract. See the javadoc: http://docs.oracle.com/javase/6/docs/api/javax/swing/filechooser/FileFilter.html
回答3:
Putting it all into a concise form, here is a flexible file chooser routine. It specifies initial directory and file type and it furnishes the result both as a file or a complete path name. You may also want to set your entire program into native interface mode by placing the setLookAndFeel command at the Main entry point to your program.
String[] fileChooser(Component parent, String dir, String typeFile) {
File dirFile = new File(dir);
JFileChooser chooser = new JFileChooser();
// e.g. typeFile = "txt", "jpg", etc.
FileNameExtensionFilter filter =
new FileNameExtensionFilter("Choose a "+typeFile+" file",
typeFile);
chooser.setFileFilter(filter);
chooser.setCurrentDirectory(dirFile);
int returnVal = chooser.showOpenDialog(parent);
String[] selectedDirFile = new String[2];
if(returnVal == JFileChooser.APPROVE_OPTION) {
// full path
selectedDirFile[0] = chooser.getSelectedFile().getPath();
// just filename
selectedDirFile[1] = chooser.getSelectedFile().getName();
}
return selectedDirFile;
}
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch (Exception e) {
e.printStackTrace();
}
来源:https://stackoverflow.com/questions/15954770/starting-a-jfilechooser-at-a-specified-directory-and-only-showing-files-of-a-spe