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

2012/7/7

JFileChooser

darkmist255 darkmist255

2012/7/7

#
Does anyone have any experience with swing's JFileChooser? I'm making a map maker so that I can have an easier time making the maps for the game I'm working on. All is working fine, except I have to hard-code the files to open. Does anyone know how to create and open a JFileChooser window and save the selected file to a String/File? I seem to be hopelessly lost after a few hours of googling with no success. Edit: Well I've got a chooser to come up now, but can't figure out how to get a FileFilter working. If anyone happens to know, I'd be happy to hear how. Further edit: This is my attempt, but it tells me it "cannot be applied to given types; required: javax.swing.filechooser.FileFilter; found: <anonymous java.io.FileFilter>;" I don't see how I can make it make a swing.filechooser FileFilter and not an io FileFilter.
public class loadButton extends clickableButton
{
    public void mouseClickAction()
    {
        JFileChooser fc = new JFileChooser();
        Frame fcframe = new Frame();
        fc.setFileFilter(new FileFilter()
            { public boolean accept(File f)
                {
                    if(f.isDirectory()) return true;
                    String[] extension = {".ethrmap", ".txt"};
                    for(int i = 0; i < extension.length; i++)
                    {
                        if(f.getName().toLowerCase().endsWith(extension[i])) return true;
                    }
                    return false;
                }
            });
        int returnVal = fc.showOpenDialog(fcframe);
        if(returnVal == JFileChooser.APPROVE_OPTION)
        {
            File selFile = fc.getSelectedFile();
            System.out.println(selFile.toString());
        }
    }
}
darkmist255 darkmist255

2012/7/7

#
Once again, I have solved my problem a short time after giving up and posting it on the forums :D. Here is the working code I ended up using.
public class loadButton extends clickableButton
{
    public void mouseClickAction()
    {
        JFileChooser fc = new JFileChooser();
        Frame fcframe = new Frame();
        fc.setFileFilter(new javax.swing.filechooser.FileFilter()
            {   public boolean accept(File f)
                {
                    if(f.isDirectory()) return true;
                    String[] extension = {".ethrmap", ".txt"};
                    for(int i = 0; i < extension.length; i++)
                    {
                        if(f.getName().toLowerCase().endsWith(extension[i])) return true;
                    }
                    return false;
                }
                public String getDescription()
                {
                    return "ethrmap and txt files";
                }
            });
        int returnVal = fc.showOpenDialog(fcframe);
        if(returnVal == JFileChooser.APPROVE_OPTION)
        {
            File selFile = fc.getSelectedFile();
            System.out.println(selFile.toString());
        }
    }
}
You need to login to post a reply.