Re: Class within a class



Many thanks Aleksander.

Aleksandar Pecanov emailed this:
It's called an inner class. For a small help class, like the one you using, it is not a problem at
all. It's not considered hacking. Various GUI builders will use inner
classes for achieving some trivial tasks, even handing and similar.


However, you might avoid large class codes (since it is a good practice
with Java) and brake them up in lots of smaller classes based on their
functionality. I find this a much better method of doing things.

On Thu, 2005-04-28 at 13:42 +0000, MS wrote:

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

  • Re: Class within a class
    ... It's called an inner class. ... It's not considered hacking. ... > uses the File class method listFilestaking a FilenameFilter. ... > public class FileRename extends javax.swing.JFrame ...
    (comp.lang.java.help)
  • Null pointer exceptions
    ... Entering method listAllFiles ... ... FilenameFilter selected. ... String extension) ...
    (comp.lang.java.programmer)
  • Re: List of files with given extension in a directory
    ... implementing Filenamefilter to search for directories, ... final String fileSep = File.separator; ... public boolean accept{ ... public class FilenameFilterTest { ...
    (comp.lang.java.programmer)
  • Class within a 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 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)