Click here to Skip to main content
15,884,176 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 system....in order to assgin different icons to each node i'm overriding getTreeCellRendererComponent in the following manner

C#
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);
       }
    } 
    else  
    {
      filename = nodefile.getName();
    }
  }
  JLabel result = (JLabel) super.getTreeCellRendererComponent(tree,filename, sel, expanded, leaf, row, hasFocus);
  if (nodefile != null) 
  {
    icon = fsv.getSystemIcon(nodefile);
    result.setIcon(icon);
  }
  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 java.io.File<br />
at line:File nodefile=(File)dmtn.getUserObject();<br />

please help me with this code and do point out my mistakes
thanks
Posted
Updated 15-Jun-11 18:28pm
v3
Comments
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
 
v2
Comments
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 through.it 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
[Code]myrenderer.setBackgroundSelectionColor(Color.blue);[/Code]

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

MSIL
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)
          .getUserObject();
      System.out.println("node is dmtn");
      if(userObject instanceof File)
      {
          System.out.println("value is file");
          File tempfile=(File)userObject;
         this.setText(tempfile.getName());
          this.setIcon(fsv.getSystemIcon(tempfile));
      }
         }

        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