Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Java Swing
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
 
 
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
at line:File nodefile=(File)dmtn.getUserObject();

please help me with this code and do point out my mistakes
thanks
Posted 15-Jun-11 10:47am
Edited 15-Jun-11 19:28pm
v3
Comments
code in play at 16-Jun-11 0:29am
   
Edited for adding pre tag and formatting
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
v2
Comments
ankit_khedekar at 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 at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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)
          .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;
    }
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 584
1 Maciej Los 275
2 DamithSL 233
3 Sergey Alexandrovich Kryukov 209
4 BillWoodruff 200
0 OriginalGriff 6,803
1 Sergey Alexandrovich Kryukov 6,377
2 DamithSL 5,421
3 Manas Bhardwaj 4,841
4 Maciej Los 4,330


Advertise | Privacy | Mobile
Web02 | 2.8.1411023.1 | Last Updated 17 Jun 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100