Results 1 to 10 of 10
  1. #1
    heartysnowy's Avatar
    heartysnowy is offline Member
    Join Date
    Nov 2008
    Posts
    37
    Rep Power
    0

    Default File Extension Filter

    Hi all,

    I would like to see only ".txt" extention only when I open my fileChooser dialog from my application.
    Do I need to extends FileFilter class to my main class?
    I already had JFrame class extended to my main class ......:( so how should I solve this out?

    Thanks,
    Hearty

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Yes you have to extend the FileFilter class into your application. It's all about OO lol.

    I'll explain it in this way,

    You have a class extend JFrame, it's fine. Then write another class for processing on files using FileFiler extended. On a click event call showOpenDialog()
    Last edited by Eranga; 11-26-2008 at 05:05 AM.

  3. #3
    serjant's Avatar
    serjant is offline Senior Member
    Join Date
    Jun 2008
    Location
    Ukraine,Zaporozhye
    Posts
    487
    Rep Power
    7

    Default

    This is the file Filter when you open JFIleCHooser
    Java Code:
    
                   JFileChooser chooser = new JFileChooser();
    	
    		//sets the current directory of the file chooser that will be shown in file chooser
    		chooser.setCurrentDirectory(new File("$HOME"));
    		
    		//sets the rule of files that will be shown in file chooser
    		//only txt files are shown
    		chooser.setFileFilter(new javax.swing.filechooser.FileFilter() {
    	        public boolean accept(File f) {
    	          String name = f.getName().toLowerCase();
    	          
    	          return name.endsWith(".txt");
    	        }
    
    	        public String getDescription() {
    	          return "Text files";
    	        }
    	      });
    		int status=chooser.showOpenDialog(null);
    		//checks what was the option button clicked in JFileChooser
    		if(status!=JFileChooser.APPROVE_OPTION){
    .............................................................................
    Last edited by serjant; 11-26-2008 at 07:04 AM.

  4. #4
    heartysnowy's Avatar
    heartysnowy is offline Member
    Join Date
    Nov 2008
    Posts
    37
    Rep Power
    0

    Default

    Thanks for the help...i will work try them on my codes..

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,404
    Rep Power
    20

    Default

    Why reinvent the wheel?
    FileNameExtensionFilter (Java Platform SE 6)

    And to disallow other files from being shown/selectable
    Java Code:
    fileChooser.setAcceptAllFileFilterUsed(false);
    db

  6. #6
    heartysnowy's Avatar
    heartysnowy is offline Member
    Join Date
    Nov 2008
    Posts
    37
    Rep Power
    0

    Default

    Yup, practically it works perfectly, but its abit user-unfriendly in a sense that when you select "Text Files", u can't see the directories anymore for further locate the text files.
    Its a bit hassle to keep selecting "All files" to see the folders and "Text Files" to only see the .txt files.
    Would there be a way to display both 'Directories and Filtered File Extention".

    i tried to add this but it doesn't work..
    chooser.setFileSelectionMode(chooser.FILES_AND_DIR ECTORIES);

    Thanks for the help .:)

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,404
    Rep Power
    20

    Default

    If you're still suck with customizing FileFilter, in the code posted by serjant change this line
    Java Code:
     return name.endsWith(".txt");
    to
    Java Code:
     return name.endsWith(".txt") || f.isDirectory();
    Your asking the question leads me to think that you just want code that you can use blindly. That attitude won't get you far. You have to take the time and effort to understand the tips you get here.

    In this case, understanding what is done with the boolean returned from the accept() method.

    What's wrong with using FileNameExtensionFilter anyway? It encapsulates the functionality you require.

    db

  8. #8
    morganmora is offline Member
    Join Date
    Oct 2010
    Posts
    1
    Rep Power
    0

    Default

    bether use this:

    javax.swing.filechooser.FileNameExtensionFilter filter = new javax.swing.filechooser.FileNameExtensionFilter("j pg images", "jpg");

  9. #9
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,404
    Rep Power
    20

    Default

    Quote Originally Posted by morganmora View Post
    bether use this:

    javax.swing.filechooser.FileNameExtensionFilter filter = new javax.swing.filechooser.FileNameExtensionFilter("j pg images", "jpg");
    Boo hoo! The question was asked (and answered) nearly 2 years ago.

    And the requirement wasn't for a filter to show JPEG image files.

    db

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. web content filter or internet filter
    By sundarjothi in forum Advanced Java
    Replies: 3
    Last Post: 05-15-2008, 11:36 AM
  2. Replies: 0
    Last Post: 04-05-2008, 10:13 AM
  3. Filter a file in java (unix cut command equivalent)
    By marcosabel in forum New To Java
    Replies: 0
    Last Post: 02-11-2008, 07:26 PM
  4. Regex for file extension
    By gapper in forum New To Java
    Replies: 1
    Last Post: 01-31-2008, 03:59 PM
  5. How to filter files in file upload using html contorl
    By deivaganesh in forum Advanced Java
    Replies: 0
    Last Post: 01-29-2008, 06:31 AM

Posting Permissions

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