Class within a class



Hi,

I'm not sure what it is called but in Java you can define a class inside another class.

I have a GUI (extends javax.swing.JFrame) which, among other things, allows some file selections based on a file extension, to achieve this it uses the File class method listFiles() taking a FilenameFilter.

FilenameFilter is an interface with a single method to define called accept(File dir, String name). Rather than creating a new file containing a class of one method which implements FilenameFilter I've put that class inside my GUI class like this:

public class FileRename extends javax.swing.JFrame
{
    public FileRename()
    {
        initComponents();
    }

....lots of methods...followed by the class within the class here:

private class FNFilter implements FilenameFilter
{
    private String fileExtension;
    public FNFilter(String extension)
    {
        fileExtension = extension;
    }

    public boolean accept(File dir, String name)
    {
        File checkDir = new File(dir, name);
        if (checkDir.isDirectory() == true)
        {
            return false;
        }

        name = name.toLowerCase();
        fileExtension = fileExtension.toLowerCase();

        if (name.endsWith(fileExtension) == true)
        {
            return true;
        }
        return false;
    }
}  // End of FNFilter class

}  // End of FileRename class


Is this kind of coding generally acceptable or is it considered messy or a bit of a hack? If so, how should it be done?


Thanks,

MS
.



Relevant Pages

  • Null pointer exceptions
    ... Entering method listAllFiles ... ... FilenameFilter selected. ... String extension) ...
    (comp.lang.java.programmer)
  • Re: Class within a class
    ... It's not considered hacking. ... I have a GUI which, among other things, allows some file selections based on a file extension, to achieve this it uses the File class method listFilestaking a FilenameFilter. ... FilenameFilter is an interface with a single method to define called accept(File dir, String name). ...
    (comp.lang.java.help)
  • java.awt.FileDialog und FilenameFilter
    ... FilenameFilter filter = new FilenameFilter{ ... Es wird kein Filter gesetzt und auch kein Startdirectory. ... da wenn das Microsoft-Native-Control ... verwendet wird die Angabe des Filters mittels einem besonderen String ...
    (de.comp.lang.java)
  • Re: alle Files in Ordner nach Name durchsuchen ?!
    ... Alfred schrieb: ... Wozu gibt es eigentlich FilenameFilter? ... public boolean accept(File dir, String name) { ... search.java:97: cannot resolve symbol ...
    (de.comp.lang.java)