简体   繁体   中英

JFileChooser Add different file types in filter

I have a JFileChooser and i want to have different options available in the type which will change the extension. The options i want are

  • .txt
  • .html
  • .xml

Right now I have:

    JFileChooser chooser = new WritableFileChooser(Model.getSingleton().getOptionsParam().getUserDirectory());

    chooser.setFileFilter(new FileFilter()
    {

        @Override
        public boolean accept(File file)
        {
            if (file.isDirectory())
            {
                return true;
            }
            else if (file.isFile())
            {
                String lcFileName = file.getName().toLowerCase(Locale.ROOT);
                return (lcFileName.endsWith(TXT_FILE_EXTENSION) || lcFileName.endsWith(HTML_FILE_EXTENSION) || lcFileName.endsWith(XML_FILE_EXTENSION)                                          }
            return false;
        }

        @Override
        public String getDescription()
        {
            return Constant.messages.getString("file.format.html");
        }

But only All files and HTML are available in the file type filter. Ideally, i also want to get rid of the All files options.

Also i have two different format .html's that are to be generated, is there any indicator i can add so that the file chooser is smart enough to know which one i want?

As @AndrewMcCoist said, the Oracle Tutorial on Filters is somewhat helpful but i got my answer and solution from this example .

        chooser .addChoosableFileFilter(new FileNameExtensionFilter("PDF Documents", "pdf"));
        chooser .addChoosableFileFilter(new FileNameExtensionFilter("MS Office Documents", "docx", "xlsx", "pptx"));
        chooser .addChoosableFileFilter(new FileNameExtensionFilter("Images", "jpg", "png", "gif", "bmp"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM