问题
I am having slight trouble with JFileChooser
. I want to restrict to only read certain kind of files. Another stack answer recommended using a FileNameExtensionFilter
, but this doesn't seem to be working. This might be because I have to restrict it to a custom file type called a "battle" file. The filter is not recognizing this file type since it is not a commonly used file type. the assignment I am doing requires this and therefore I have to use this type of file. On reaching the directory, it doesn't allow me to choose the file.
Following is my code snippet:
public class battleship_window extends JFrame implements ActionListener{
JLabel loglabel;
JButton selectbutton;
JButton startbutton;
JLabel filename;
File file;
//JLabel scorearray[]=new JLabel[10];
char alphabet[]={'A','B','C','D','E','F','G','H','I','J'};
battleship_window()
{
super("Battleship");
setSize(1050,550);
setLocation(50,200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new BorderLayout());
JPanel logpanel=new JPanel();
makelogpanel(logpanel);
this.add(logpanel,BorderLayout.SOUTH);
this.setVisible(true);
}
public void makelogpanel(JPanel logpanel)
{
loglabel=new JLabel("Log: You are now in edit mode, click to place your ships");
selectbutton=new JButton("Select File");
startbutton=new JButton("Start");
startbutton.setEnabled(false);
filename=new JLabel("File:");
logpanel.setLayout(new BoxLayout(logpanel,BoxLayout.X_AXIS));
logpanel.add(loglabel);
logpanel.add(selectbutton);
logpanel.add(filename);
logpanel.add(startbutton);
selectbutton.addActionListener(this);
}
public static void main(String [] args)
{
battleship_window bw=new battleship_window();
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==selectbutton)
{
JFileChooser fileDialog=new JFileChooser();
fileDialog.setAcceptAllFileFilterUsed(false);
FileNameExtensionFilter filter = new FileNameExtensionFilter("Battle file", "battle");
fileDialog.addChoosableFileFilter(filter);
int returnVal = fileDialog.showOpenDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
file = fileDialog.getSelectedFile();
filename.setText("File:" + file.getName());
}
}
}
}
How can I achieve this either through the above method or any other method? Please note that the code compiles and i delted some of my code above to make it more readable so there might be some syntax errors in the above code.
回答1:
I actually have a program I am writing at the moment that has multiple JFileChooser's, each of them requiring to look for only specific file types. This example would allow you to have the same idea, so that if in the future, you need to allow for different file types, you are ready to go. I have created a custom class that extends upon FileFilter
public class CustomExtension extends FileFilter
{
private String type;
public CustomExtension(String type)
{
this.type = type;
}
public Boolean accept(File file)
{
if(file.isDirectory())
return true;
String ext = getExtension(file);
if(ext == null)
return false;
switch(type)
{
case "battle":
if(ext.equals("battle"))
return true;
else
break;
default:
System.out.println(type + " has not been set up in the switch statement yet");
}
return false;
}
public String getDescription()
{
switch(type)
{
case "battle":
return "Only battle file supported";
}
}
public String getExtension(File f)
{
String ext = null;
String filename = f.getName();
int i = filename.lastIndexOf('.');
if(i > 0 && i < filename.length() - 1)
ext = s.substring(i + 1).toLowerCase();
return ext;
}
}
I have been using this for a while now and I haven't noticed any bugs. To set up a JFileChooser so that it uses this filter, you would use:
JFileChooser chooser = new JFileChooser();
chooser.setFileFilter(new CustomExtension("battle"));
Now your JFileChooser will only display directories, and files that end in .battle
回答2:
It doesn't need to be commonly used file type to regednize with file chooser.
Just create a file and rename it with extention to:
"My File.battle"
Then browse with your program. It will show the file.
Or try this code:
JFileChooser fileDialog = new JFileChooser();
fileDialog.setAcceptAllFileFilterUsed(false);
int returnVal = fileDialog.showOpenDialog(null);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fileDialog.getSelectedFile();
if(file.getName().endsWith(".battle")){
System.out.println("Selected file is battle file");
}else{
System.out.println("Invalied file type");
}
}
回答3:
Try to use
fileDialog.setFileFilter(new FileNameExtensionFilter("", "battle"));
instead of
fileDialog.addChoosableFileFilter(filter);
The addChoosableFileFilter
will get added in the JFileChooser's combo box named Files of Type
.
来源:https://stackoverflow.com/questions/28757272/how-to-restrict-a-jfilechooser-to-a-custom-file-type