Member Avatar for Illidanek

Ok so i have been looking this up on the internet but all the stuff i've been finding has been for other stuff or worked in a different way than i wanted it to.

So what i need is a Filetype filter for a FileDialog (not a JFileDialog or anything). The program i have will allow the user to browse for mp3 files, so i need the filter to only show the user those, and it will also allow the user to save mp3 files, so i need it to only allow the user to save the file as a .mp3 extension.

Here's the code which opens the filedialog and pastes the file path into a JTextField 'browsed'.

protected void onBrowse() // opens the browse and sets the browse text field 
	{
		FileDialog fileDialog = new FileDialog(this, "Open/Save");
        fileDialog.show();
        if (fileDialog.getFile() != null)
        {
            String directory = fileDialog.getDirectory();
            if (!directory.endsWith(File.separator)) 
            {
                directory += File.separator;
            }
            String file = fileDialog.getFile();
            browsed.setText(directory + file);
        }
	}

Please give any suggestions about filters for this sort of code. Thx

Recommended Answers

All 5 Replies

google FileFilter~~

Member Avatar for Illidanek

google FileFilter~~

I'll reply with a quote

Ok so i have been looking this up on the internet but all the stuff i've been finding has been for other stuff or worked in a different way than i wanted it to.

Create a class that extends FilenameFilter (not FileFilter) (and it can even, easily, be an anonymous inner class) and use the setFilenameFilter method of FileDialog. Let's see your attempt at that (BTW a quick look at the API docs should have pointed this out, FileDialog only has about 10 methods). Simply have the "accept" method return true for filenames ending with "mp3" as a simple explanation. Try it, and post your attempt if you can't get it to work.

I do, however, need to ask why you wish to use the awt FileDialog rather than the Swing JFileChooser. I hope you're not mixing awt and swing elements.

commented: Helpful and on-topic +1
Member Avatar for Illidanek

Create a class that extends FilenameFilter (not FileFilter) (and it can even, easily, be an anonymous inner class) and use the setFilenameFilter method of FileDialog. Let's see your attempt at that (BTW a quick look at the API docs should have pointed this out, FileDialog only has about 10 methods). Simply have the "accept" method return true for filenames ending with "mp3" as a simple explanation. Try it, and post your attempt if you can't get it to work.

I do, however, need to ask why you wish to use the awt FileDialog rather than the Swing JFileChooser. I hope you're not mixing awt and swing elements.

Ok dude, thx, so I made a FilenameFilter class like this:

import java.io.FilenameFilter;
import java.io.File;
public class MP3Filter implements FilenameFilter {
    public boolean accept(File dir, String name) {
        return (name.endsWith(".mp3"));
    }
}

and then i said this, in the onBrowse() method.

FileDialog fileDialog = new FileDialog(this, "Open/Save");
		MP3Filter filter = new MP3Filter();
		fileDialog.setFilenameFilter(filter);
        fileDialog.show();

BUt the thing is that it compiles and runs fine, but it doesn't do anything, and when i open the filedialog in the program, it still says "All Files" and nothing else and i can select any type of file

Then I can only assume your on Windows, and the API docs for setFilenameFilter says

Filename filters do not function in Sun's reference implementation for Microsoft Windows.

Switch to JFileChooser, or live with it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.