I just need some help figuring out how to solve this, cause I'm stuck.
Error: java.lang.Comparable cannot be inherited with different
arguments: <> and <java.io.File>
<------------ Code ------------>
import java.util.*;
import java.io.File;
import java.text.Collator;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;
import javax.swing.event.TreeModelListener;
import javax.swing.event.TreeModelEvent;
private class FileTreeNode extends File implements Comparable
{
public FileTreeNode(File file)
{
super(file, "");
}
/**
* Compare two FileTreeNode objects so that directories
* are sorted first.
*
* @param object Object to compare to.
* @return Compare identifier.
*/
public int compareTo (Object object)
{
File file1 = this;
File file2 = (File) object;
Collator collator = Collator.getInstance();
if (file1.isDirectory() && file2.isFile())
return -1;
else if (file1.isFile() && file2.isDirectory())
return +1;
else
return collator.compare(file1.getName(), file2.getName());
}
Oliver Wong - 23 Nov 2006 22:19 GMT
>I just need some help figuring out how to solve this, cause I'm stuck.
>
> Error: java.lang.Comparable cannot be inherited with different
> arguments: <> and <java.io.File>
[...]
> private class FileTreeNode extends File implements Comparable
Basically, the error says that FileTreeNode cannot implement both
Comparable and Comparable<File>. It implements Comparable<File> indirectly
via File.
What's the purpose of the FileTreeNode class?
- Oliver
inanetheory@gmail.com - 23 Nov 2006 22:31 GMT
> What's the purpose of the FileTreeNode class?
>
> - Oliver
I'm trying to make a JTree that shows me the Windows roots (ex. C:\
D:\)
I found some GPL source code on the internet that does this for me so
I really have not researched the code thoroughly enough.
(http://geosoft.no/software/filesystem/FileSystemModel.java.html)
If I change: private class FileTreeNode extends File implements
Comparable
To: private class FileTreeNode extends File
it says name clash: compareTo(java.lang.Object) in
no.geosoft.cc.directory.FileSystemModel.FileTreeNode and compareTo(T)
in java.lang.Comparable<java.io.File> have the same erasure, yet
neither overrides the other
Oliver Wong - 23 Nov 2006 22:40 GMT
>> What's the purpose of the FileTreeNode class?
>>
>> - Oliver
>
> I'm trying to make a JTree that shows me the Windows roots (ex. C:\
> D:\)
If you're not adding any additional functionality to File, and just want
to change the order they display in, I recommend you use an external
comparator, rather than extending File.
- Oliver
Hendrik Maryns - 24 Nov 2006 11:16 GMT
inanetheory@gmail.com schreef:
>> What's the purpose of the FileTreeNode class?
>>
[quoted text clipped - 17 lines]
> in java.lang.Comparable<java.io.File> have the same erasure, yet
> neither overrides the other
Of course. Your method is compareTo(Object), but you inherit
compareTo(File) from file. Name clash. Change Object to File and throw
out the cast.
H.
- --
Hendrik Maryns
http://tcl.sfs.uni-tuebingen.de/~hendrik/
==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
Hendrik Maryns - 24 Nov 2006 11:18 GMT
inanetheory@gmail.com schreef:
> I just need some help figuring out how to solve this, cause I'm stuck.
>
[quoted text clipped - 12 lines]
> import javax.swing.event.TreeModelEvent;
> private class FileTreeNode extends File implements Comparable
File implements Comparable<File>, so now you claim to implement both
that and the non-generic Comparable. Change it to implements
Comparable<? super FileTreeNode>.
And see my other post, but instead of File, give as argument
FileTreeNode. Even better, listen to Oliver.
H.
- --
Hendrik Maryns
http://tcl.sfs.uni-tuebingen.de/~hendrik/
==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
Daniel Pitts - 24 Nov 2006 18:25 GMT
> I just need some help figuring out how to solve this, cause I'm stuck.
>
[quoted text clipped - 39 lines]
> return collator.compare(file1.getName(), file2.getName());
> }
My first suggestion is that you change your FileTreeNode from "is a
File" to "Has a File". And make it comparable to its own type.
private class FileTreeNode implements Comparable<FileTreeNode> {
private final File file;
public FileTreeNode(File file) {
this.file = file;
}
/**
* Compare two FileTreeNode objects so that directories
* are sorted first.
*
* @param object Object to compare to.
* @return Compare identifier.
*/
public int compareTo (FileTreeNode object) {
final File file1 = this.file;
final File file2 = object.file;
Collator collator = Collator.getInstance();
if (file1.isDirectory() && file2.isFile())
return -1;
else if (file1.isFile() && file2.isDirectory())
return +1;
else
return collator.compare(file1.getName(), file2.getName());
}
}
Hope this helps.
Daniel.
Christian Kaufhold - 24 Nov 2006 18:42 GMT
> public int compareTo (Object object)
> {
> File file1 = this;
> File file2 = (File) object;
> Collator collator = Collator.getInstance();
> if (file1.isDirectory() && file2.isFile())
> return -1;
[quoted text clipped - 3 lines]
> return collator.compare(file1.getName(), file2.getName());
> }
Using this comparison function is unsafe, since a File may change its
isFile/isDirectory status during usage (e.g.e sorting).
Christian
inanetheory@gmail.com - 24 Nov 2006 19:12 GMT
Thanks to everyone who helped here and after trying out your methods, I
decided to scrap this altogether and write my own model.