Results 1 to 5 of 5
  1. #1
    xbox_nutter is offline Member
    Join Date
    Mar 2009
    Posts
    15
    Rep Power
    0

    Default Hi please help with a file filter!

    Hi am having a problem with a file filter.
    I need the filter to ONLY SHOW .xml files....... here is my code:

    Java Code:
    public class loadXML implements ActionListener {
    		public void actionPerformed(ActionEvent e) {
    			final JFileChooser _fileChooser = new JFileChooser();
    		}
    
    	}
    
    	public class ff extends javax.swing.filechooser.FileFilter {
    		@Override
    		public boolean accept(File f) {
    			try {
    				if (f.isDirectory() || f.getName().toLowerCase().endsWith("xml")) {
    					System.out.println("This file is accepted and ends with XMl");
    					return true;
    				} else {
    					System.out.println("This file is NOT excepted because its not an XML file");
    //					JOptionPane
    //							.showMessageDialog(main,
    //									"This file is not an XML file please select an XML file and continue");
    					
    				}
    			} catch (Exception e) {
    				
    			}
    			return false;
    		}
    
    		@Override
    		public String getDescription() {
    			return ".xml files";
    		}
    	}
    however for some reason this shows ALL files and directories and lets the user select any file

    Please help I have been trying to get this to work for ages now and would really appreciate help in finally putting this project to rest !

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    I see where you define a Filter Class, but not where you use it. P.S. it is JFileChooser itself which needs it, and it will then only present those files to the user that fir the description, so yoou definately do not need, and should not have, that JOptionPane in there.

  3. #3
    xbox_nutter is offline Member
    Join Date
    Mar 2009
    Posts
    15
    Rep Power
    0

    Default

    Hi thanks for the quick reply:

    I have taken what you said into account and made the relevent changes......

    However it is still showing ALL files instead fo JUST showing xml files??

    here is my new code:

    Java Code:
    	public class ff extends javax.swing.filechooser.FileFilter {
    		boolean except = false;
    		@Override
    		public boolean accept(File f) {
    			if (f.isDirectory() || f.getName().toLowerCase().endsWith("xml")) {
    				except = true;
    			} else {
    				System.out
    						.println("This file is NOT excepted because its not an XML file");
    				except = false;
    			}
    			return except;
    		}
    		@Override
    		public String getDescription() {
    			return ".xml files";
    		}
    	}
    
    	public class loadXML implements ActionListener {
    		public void actionPerformed(ActionEvent e) {
    			// Create a file chooser
    			final JFileChooser _filechooser = new JFileChooser();
    			// In response to a button click:
    			int returnVal = _filechooser.showOpenDialog(main);
    			_filechooser.setFileFilter(new ff());
    
    		}
    	}
    Any ideas would be awsome!
    Thanks !

  4. #4
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Don't you think you should set the FileFilter before opening the Dialog?
    Since it is the FileFilter that will modify the Dialog.
    And how should it do that if it isn't related to the Dialog until after the Dialog has already been used?

    Edit: P.S. For debuggin once or twice, leave the print statement in, otherwise, you are going to want to remove that, too.

  5. #5
    xbox_nutter is offline Member
    Join Date
    Mar 2009
    Posts
    15
    Rep Power
    0

    Default

    YOU SIR ARE A GENIUS

    Works like a charm thanks you very much for all your
    help!

Similar Threads

  1. File Extension Filter
    By heartysnowy in forum New To Java
    Replies: 9
    Last Post: 10-09-2010, 02:33 PM
  2. web content filter or internet filter
    By sundarjothi in forum Advanced Java
    Replies: 3
    Last Post: 05-15-2008, 12:36 PM
  3. Filter a file in java (unix cut command equivalent)
    By marcosabel in forum New To Java
    Replies: 0
    Last Post: 02-11-2008, 08:26 PM
  4. How to filter files in file upload using html contorl
    By deivaganesh in forum Advanced Java
    Replies: 0
    Last Post: 01-29-2008, 07:31 AM
  5. Need a filter
    By revathi17 in forum New To Java
    Replies: 1
    Last Post: 08-08-2007, 08:37 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
  •