Error updating code to 1.5

I

inanetheory

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());
}
 
O

Oliver Wong

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
 
I

inanetheory

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
 
O

Oliver Wong

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
 
H

Hendrik Maryns

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

(e-mail address removed) schreef:
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

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
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)

iD8DBQFFZtSFe+7xMGD3itQRAuOgAJ9qGNFVe1OtcYE3pS4qK8sykPQVqgCfXu5D
7mQ/k6N73qnN6WNCpPodOmg=
=UPWg
-----END PGP SIGNATURE-----
 
H

Hendrik Maryns

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

(e-mail address removed) schreef:
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

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
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)

iD8DBQFFZtTxe+7xMGD3itQRAm21AJwN1h8TQ4lxjyAQHJzlzSSVz5hUsQCfavbm
6VHFh0llw8snyPh9fZP4Qzc=
=PDK+
-----END PGP SIGNATURE-----
 
D

Daniel Pitts

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());
}

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.
 
C

Christian Kaufhold

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());
}

Using this comparison function is unsafe, since a File may change its
isFile/isDirectory status during usage (e.g.e sorting).


Christian
 
I

inanetheory

Thanks to everyone who helped here and after trying out your methods, I
decided to scrap this altogether and write my own model.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,266
Messages
2,571,075
Members
48,772
Latest member
Backspace Studios

Latest Threads

Top