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

    Default problem with File Filters

    Hi is there anyone out there who can explain (in an easy language)

    How a file filter works please?

    I have a file chooser and I only want it to display directories and .xml files

    I have read some stuff on the sun website but not making a great deal of sense atm :confused:

    Thanks People !

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

    Default

    Instead of reinventing the wheel, use javax.swing.filechooser.FileNameExtensionFilter

    db

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

    Default

    thanks for quick reply !
    and how would i use that as i cannot seem to find it?

    Thanks again!

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

    Default

    Hi all I have had a read thorugh but still cannot see how this is suppose to work properly ?

    here is my code:
    Java Code:
    	public class loadXML implements ActionListener {
    		public void actionPerformed(ActionEvent e) {
    			JFileChooser _filechooser = new JFileChooser();
    			_filechooser
    					.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
    			_filechooser.showOpenDialog(main);
    			_filechooser.setAcceptAllFileFilterUsed(false);
    			_filechooser.setFileFilter(new ff());
    			File file_to_open = _filechooser.getSelectedFile();
    			setoutput(file_to_open.getName());
    		}
    
    	}
    
    	public class ff extends javax.swing.filechooser.FileFilter {
    		@Override
    		public boolean accept(File f) {
    			if (f.isDirectory() || f.getName().toLowerCase().endsWith(".xml")) {
    				System.out.println("This file is accepted and ends with XMl");
    				return true;
    			} else {
    				JOptionPane
    						.showMessageDialog(main,
    								"This file is not an XML file please select an XML file and continue");
    				return false;
    			}
    		}
    
    		@Override
    		public String getDescription() {
    			return ".xml files";
    		}
    	}
    I want the only files to show up to be directories and .xml files any one know what is wrond with my code?

    thanks?

Similar Threads

  1. Filters in java
    By tskumarme in forum New To Java
    Replies: 0
    Last Post: 08-05-2008, 04:12 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
  •