Click here to Skip to main content
11,928,587 members (56,093 online)
Rate this:
Please Sign up or sign in to vote.
See more: Java Swing
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
at line:File nodefile=(File)dmtn.getUserObject();

please help me with this code and do point out my mistakes
Posted 15-Jun-11 10:47am
Edited 15-Jun-11 19:28pm
code in play 16-Jun-11 0:29am
Edited for adding pre tag and formatting
Rate this: bad
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.
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
Rate this: bad
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)
      System.out.println("node is dmtn");
      if(userObject instanceof File)
          System.out.println("value is file");
          File tempfile=(File)userObject;
        return this;

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.151126.1 | Last Updated 17 Jun 2011
Copyright © CodeProject, 1999-2015
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