限制为自定义文件类型

限制为自定义文件类型

本文介绍了如何将 JFileChooser 限制为自定义文件类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 JFileChooser 时遇到了一些小问题.我想限制只能读取某些类型的文件.建议使用 FileNameExtensionFilter 的另一个堆栈答案,但这似乎不起作用.这可能是因为我必须将其限制为称为战斗"文件的自定义文件类型.过滤器无法识别此文件类型,因为它不是常用的文件类型.我正在做的作业需要这个,因此我必须使用这种类型的文件.到达目录时,它不允许我选择文件.

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.

以下是我的代码片段:

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.

推荐答案

我现在正在编写的程序实际上有多个 JFileChooser,每个程序只需要查找特定的文件类型.这个例子会让你有同样的想法,这样如果将来你需要允许不同的文件类型,你就可以开始了.我创建了一个扩展到 FileFilter 的自定义类

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;
    }
}

我已经使用了一段时间了,我没有注意到任何错误.要设置 JFileChooser 以使用此过滤器,您可以使用:

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"));

现在您的 JFileChooser 将只显示以 .battle 结尾的目录和文件

Now your JFileChooser will only display directories, and files that end in .battle

这篇关于如何将 JFileChooser 限制为自定义文件类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 15:29