• Post Reply Bookmark Topic Watch Topic
  • New Topic

TreeNode ClassCastException?

 
Max Power
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello, whats wrong with this code:



why that? this code is from a book so i think it is correct.

THANKS
 
Stefan Krompass
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

the method getRoot() of interface javax.swing.tree.TreeModel returns an instance of class java.lang.Object. So the object returned is not necessarily an instance of class javax.swing.tree.TreeNode. Can you please post a working sample where we can reproduce your problem?

Stefan
 
Max Power
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi, here it is. this code displays the filesystem inside a jtree. just start it with some existing path like c:\. after 3 seconds a method is called and the exception occurs.....


i can send you this code as well....?

THANKS IN ADVANCE :-)
 
Stefan Krompass
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Andreas Golchert:


This is the solution: Your getRoot()-method returns an instance of class File...

Stefan
 
Max Power
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi, do you know how to change this?

thanks:-)
 
Stefan Krompass
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

what you have to do is distinguish between the types of nodes you click on... (leaf -> file, non-leaf -> directory). You could try using DefaultMutableTreeNode as a "wrapper" around your file-object. Just as a few suggestions...

Stefan
 
Max Power
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi, thanks. can you go a bit more into detail?

i am still stuck...
 
Stefan Krompass
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

my solution yould look something like this. What I did, is to wrap the File-objects in DefaultMutableTreeNodes. We have to do this to separate the model (the File-objects) from the view (the nodes of the JTree).



Stefan
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!