Using FileDialogs

Hello, I am using the FileDialog to open a file, heres my code:


        FileDialog fileDialog = new FileDialog(this, "Save As...", FileDialog.SAVE);
        
        fileDialog.setFile("untitled.nek");
        
       
        fileDialog.show();
        
        if (fileDialog.getFile() == null)
            return;
        mFileName = fileDialog.getDirectory() + File.separator + fileDialog.getFile();
        
        Save(mFileName);

This is in my “SaveAs” Function. Dont worry about the Save Function. What I’m trying to do is set the file filter to ‘nek’ but I have no idea how to do this. When the file dialog appears you can save it as Anyfile, and i prefer it to be the type i want.

Any thoughts?

http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/filechooser/FileFilter.html

How to Use File Choosers:
http://java.sun.com/docs/books/tutorial/uiswing/components/filechooser.html

[quote]What I’m trying to do is set the file filter to ‘nek’ but I have no idea how to do this.
[/quote]
Theoretically, the method you’re looking for is setFilenameFilter(). However, see its Javadoc comment, it has no effect on Windows. So the AWT file dialog can’t do what you want to achieve. Either use Swing or you have to create your own implementation of a FileDialog.T

Oh… AWT… I always forget that there are still people who use it :slight_smile:

Btw usually you create the dialog once at the beginning and show each time you need it. This way it’s more responsive, generates less garbage and as a side effect you stay in the last choosen directory (that’s expected behaviour btw).

So what do i do then? Im still new to java to do some of the stuff. Does anyone know how you do it in windows?

[quote]So what do i do then? Im still new to java to do some of the stuff. Does anyone know how you do it in windows?
[/quote]
Use Swing. Then you don’t have to worry about AWT short commings.

Or check out SWT - an alternative UI framework created to support the Eclipse platform which of course works fine standalone, too. However, SWT is different to Swing and AWT, having its own merits and problems.

Here’s an SWT example.