Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Why FilenameFilter is never working?

  1. #1
    Member
    Join Date
    Jul 2011
    Posts
    53
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Why FilenameFilter is never working?

    While coding in Java, I did many programs with GUI at which I needed to save something to a file. I usually use FileDialog to save something. Sometimes I wanted to filter the names of files that I wanted to see, so I tried to use FilenameFilter. But I never managed to make it working. Usually, I do like that:

    package mainPack;
     
    import java.awt.*;
    import java.awt.event.*;
    import java .io .*;
     
    public class FerPrinc1 extends Frame implements ActionListener {
     
    	public static void main(String[] args) {
    		FerPrinc1 f1 = new FerPrinc1("Testare");
    		f1.show();
    	}
     
     
    	FerPrinc1(String  titlu)
    	{
    		super(titlu);
    		this.addWindowListener(new WindowAdapter()
    		{
    			public void windowClosing(WindowEvent e)
    			{
    				dispose();
    			}
     
    		});
    		Button b1 = new Button ("Alege fisier");
    		add(b1, BorderLayout.CENTER);
    		pack();
    		b1.addActionListener(this);
    	}
     
     
     
     
     
     
    	public void actionPerformed(ActionEvent e) {
    		FileDialog fd = new FileDialog(this, "Alege fisier", FileDialog.LOAD);
     
    		fd.setFilenameFilter(new FilenameFilter()
    		{
     
    			@Override
    			public boolean accept(File dir, String numeFis) {
     
    				return (numeFis.endsWith(".txt"));
    			}
     
    		});
    		fd.show();
    		setTitle("Fisierul ales este"+fd.getFile());
     
    	}
     
     
     
     
    }

    The piece of code with the FilenameFilter is this one:


    fd.setFilenameFilter(new FilenameFilter()
    {

    @Override
    public boolean accept(File dir, String numeFis) {

    return (numeFis.endsWith(".txt"));
    }

    });
    As I told you, I never seen this working. Any clue what I am doing wrong? Is it any problems with using this on windows 8? Or what ?


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Why FilenameFilter is never working?

    Define "not working". Not compiling or not running like you want?

    setTitle("Fisierul ales este"+fd.getFile());

    It's loading the file. However, it lets me load .java files too.

    Also, not sure what this means exactly, but it says on the FileDialog 7 API that, for setFilenameFilter()

    "Sets the filename filter for this file dialog window to the specified filter. Filename filters do not function in Sun's reference implementation for Microsoft Windows."

    You could always try JFileChooser instead of FileDialog.
    Last edited by javapenguin; June 5th, 2012 at 01:36 PM.

  3. #3
    Member
    Join Date
    Jul 2011
    Posts
    53
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Why FilenameFilter is never working?

    By not working I mean for example if i set the Loading FilenameFilter with ".txt", it will not show only .txt files as I wanted, it still shows all files in the folder!!!

  4. #4
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Why FilenameFilter is never working?

    Actually, unless I'm mistaken, what it's doing, though it isn't printing out true or false like I had it do to see if it was using the filter, which is a mystery, I think all that filter is doing is saying whether or not the selected File is of that type of extension or not.

    Here's what the API says:

    Quote Originally Posted by FillenameFilter API
    boolean accept(File dir,
    String name)

    Tests if a specified file should be included in a file list.

    Parameters:
    dir - the directory in which the file was found.
    name - the name of the file.
    Returns:
    true if and only if the name should be included in the file list; false otherwise.
    I wonder if this is what you were looking for:

    File filters with JFileChooser

    If you go the JFileChooser route, then addChooseableFileFilter() might be what you're after.

    You'd have to make a subclass of FileFilter to do that.

    Also, you could have more than one file filter I think that way too, as long as you had more than one subclass of FileFilter.

    I must confess that I don't know how to add a file filter to a FileDialog.
    Last edited by javapenguin; June 5th, 2012 at 03:23 PM.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Why FilenameFilter is never working?

    Try using the JFileChooser class. Its API doc has an example of filtering.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. not sure why the 'else is not working
    By reddevilggg in forum Loops & Control Statements
    Replies: 3
    Last Post: September 30th, 2011, 03:54 PM
  2. [SOLVED] Working on Win 7 and not on XP
    By shaumux in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 4th, 2011, 05:36 PM
  3. Why isn't this working?
    By javapenguin in forum What's Wrong With My Code?
    Replies: 14
    Last Post: January 21st, 2011, 04:08 PM
  4. Cannot seem to get this working
    By OttawaGuy in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 28th, 2010, 03:41 PM
  5. Replies: 4
    Last Post: January 27th, 2009, 12:03 AM