Click here to Skip to main content
15,942,710 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
i'm creeting a tree view for the file order to assgin different icons to each node i'm overriding getTreeCellRendererComponent in the following manner

public Component getTreeCellRendererComponent(JTree tree, Object value,boolean sel, boolean expanded, boolean leaf, int row,boolean hasFocus) 
  System.out.println("in cell renderer");
  FileSystemView fsv=FileSystemView.getFileSystemView();
  DefaultMutableTreeNode dmtn = (DefaultMutableTreeNode) value;
  File nodefile=(File)dmtn.getUserObject();//i'm getting below mentioned exception here
  Icon icon;
  String filename = "";
  if (nodefile != null) 
    if (ismynodefilesystem(nodefile)) 
       if (filename == null) 
         filename = fsv.getSystemDisplayName(nodefile);
      filename = nodefile.getName();
  JLabel result = (JLabel) super.getTreeCellRendererComponent(tree,filename, sel, expanded, leaf, row, hasFocus);
  if (nodefile != null) 
    icon = fsv.getSystemIcon(nodefile);
  return result;

i have assigned a File object to each node using setUserObject()
i'm getting an exception as:
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.lang.String cannot be cast to<br />
at line:File nodefile=(File)dmtn.getUserObject();<br />

please help me with this code and do point out my mistakes
Updated 15-Jun-11 18:28pm
pankajupadhyay29 16-Jun-11 0:29am    
Edited for adding pre tag and formatting

tried this?
File nodefile=new File(dmtn.getUserObject());

what happens if you do so?

PLease also check this site: Swing JTree Tutorial[^] Section 5 is about the icons on the tree.

EDIT: try this Showing the file system as a Swing JTree[^]

Both will not meet your exact problem - but with a bit of creativity lead you to a suitable solution.
Share this answer
ankit_khedekar 16-Jun-11 13:17pm    
File nodefile=new File(dmtn.getUserObject());

i get a compilation error:
cannot fine symbol
symbol:constructor File(java.lang.Object)

the tutorial you mentioned is something i had already gone lets you assign open-node icon,close-node icon and leaf icons i.e. all the close-nodes will have the same icon and same with open-node and leaf
whereas what want is the icon should based on the file associated with the node
ankit_khedekar 17-Jun-11 5:58am    
that was solved but now i'm stuck at this:
my tree is created perfectly and is also working perfectly,but when i click on a node the node name doesn't get highlighted.the nodes do expand and collapse on clicks
why is it can u help me

i tried this like

but this didnt work.
please help
dont know how but this solved my problem

public Component getTreeCellRendererComponent(JTree tree, Object value,
                boolean sel, boolean expanded, boolean leaf, int row,
                boolean hasFocus)
        System.out.println("value is :"+value.getClass());
        if ((value != null) && (value instanceof DefaultMutableTreeNode)) {
      Object userObject = ((DefaultMutableTreeNode) value)
      System.out.println("node is dmtn");
      if(userObject instanceof File)
          System.out.println("value is file");
          File tempfile=(File)userObject;

        return this;
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900