Results 1 to 3 of 3
  1. #1
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    356
    Rep Power
    5

    Default JFileChooser restriction

    In my Java program, I use a JFileChooser to bring in a file for processing. How can I restrict the JFileChooser to only accept a file of type .txt, or do a System.exit(1) otherwise. Or better yet, can I set it up so that the JFileChooser only displays .txt files in the window.
    Last edited by sehudson; 03-13-2011 at 07:40 PM.

  2. #2
    Join Date
    Mar 2011
    Location
    Parma Italy
    Posts
    88
    Rep Power
    0

    Default

    You can set a file filter such as:

    Java Code:
    private class TxtFileFilter extends FileFilter{
            public boolean accept(File file){
                if(file.isDirectory()) return true;
                String fname = file.getName();
                return fname.endsWith("txt");
            }
            public String getDescription(){
            return "txt file";
        }
        }
    Susanna

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,242
    Rep Power
    19

    Default

    There's already a class javax.swing.filechooser.FileNameExtensionFilter in the standard JDK. Why reinvent the wheel?

    db

Similar Threads

  1. 4 digit restriction
    By GTM in forum Java Applets
    Replies: 3
    Last Post: 12-28-2010, 02:26 AM
  2. Access Restriction error
    By kraut in forum New To Java
    Replies: 2
    Last Post: 06-19-2010, 01:29 AM
  3. Access Restriction error
    By kraut in forum Eclipse
    Replies: 0
    Last Post: 06-19-2010, 01:07 AM
  4. Replies: 7
    Last Post: 02-19-2010, 11:03 AM
  5. Error: Access restriction
    By livnihai in forum Eclipse
    Replies: 1
    Last Post: 11-25-2009, 06:53 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •