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
Aleksandar Pecanov - 28 Apr 2005 16:12 GMT
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.
> Hi,
>
[quoted text clipped - 54 lines]
>
> MS
MS - 28 Apr 2005 17:25 GMT
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
[quoted text clipped - 63 lines]
>>
>>MS