This site requires JavaScript, please enable it in your browser!
Greenfoot back
danpost
danpost wrote ...

2012/2/4

FilenameFilter in FileDialog

danpost danpost

2012/2/4

#
OK, I have figured out how to access the FileDialog window with
        FileDialog fd = new FileDialog(new Frame(),"File Picker");
        fd.setLocation(100, 100);
        fd.setVisible(true);
But, my problem is that it allows any file to be selected because the File Type dropdown box only has All Files as a valid value. How can I add a different value to the File Type dropdown box?
sp33dy sp33dy

2012/2/4

#
I've changed my response because the previous link provided was bad advice. Looking at this example: Example You need to create a FilenameFilter in order to restrict the file types you would like to filter. So, this is the FilenameFilter API (it appears to be an interface) : Filter API You then need to set the filter in your FileDialog: FileDialog API By using the setFilenameFilter method. I hope this helps. I don't have time right now to put into an example.
danpost danpost

2012/2/4

#
I had previously looked at the 'Filter', 'FilenameFilter', and 'FileDialog' APIs. I am just unsure how to set everything up to make it happen. The main problem is creating the FilenameFilter. Nothing I saw shows me how to set up a new FilenameFilter.
sp33dy sp33dy

2012/2/4

#
OK, I'll look into providing an example. Unlikely to be able to do so until tomorrow or Monday at the earliest.
danpost danpost

2012/2/4

#
I appreciate any help. Thank you in advance sp33dy.
danpost danpost

2012/2/4

#
In the getFilenameFilter and setFilenameFilter method descriptions, i see "Filename filters do not function in Sun's reference implementation for Microsoft Windows.". Does this mean that I cannot use this functionallity in Windows 7? Also, are there some cases where multiple files cannot be selected (that using setMultipleMode(true) has no effect)? I did see a method called isMultipleMode() and was wondering if that just checks the true/false value of MultipleMode or determines if MultipleMode(true/false) has any effect. I tried setMultipleMode(true) on a FileDialog instance and got compilation error 'method not found', which seemed peculiar.
sp33dy sp33dy

2012/2/5

#
Hi, I've now got this working. However, not by the mechanism you planned. I thought I'd seen this problem before and i was right. The FileDialog class is plain poor! It has to be avoided at all costs. The answer is switch to using the Swing component JFileChooser, for which is coded well. The other class has several bugs and well known issues; therefore misleading to all. I'm just about to upload a very simple demo for you to look at. You might also want to look at the JFileChooser API: JFileChooser and the filter class Filter The code in my demo is basically this:
JFileChooser jfc = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter("JPG & GIF Images", "jpg", "gif");
jfc.setFileFilter(filter);
int rc = jfc.showOpenDialog(new JFrame());

switch (rc) {
    case JFileChooser.APPROVE_OPTION:
        System.out.println("User selected file "+jfc.getSelectedFile());
        break;
    case JFileChooser.CANCEL_OPTION:
        System.out.println("User cancelled!");
        break;
    default:
        break;                
}     
which can be found in the button class. Please also note, with JFileChooser, you can set it up to do other things, i.e. multi file select etc. I hope this helps and watch out for the demo upload. Kind regards Sp33dy
sp33dy sp33dy

2012/2/5

#
Also, as mentioned in the demo posting. No file chooser type window will operate in an applet; given user security settings. I.E. You have to lower/disable settings in order to allow it to work. If you want users to be able to select files, they'll only be able to do on the local runnable version or making that setting change.
danpost danpost

2012/2/5

#
Thank you so much! And, by the way, this project is only for a close relative of mine and is for in-house use only. So, I should not have any of your aforementioned problems.
danpost danpost

2012/2/5

#
@sp33dy, I have it downloaded now, so if you wish to remove it, go ahead. Again, thank you.
You need to login to post a reply.