Results 1 to 5 of 5
  1. #1
    ankit_k is offline Member
    Join Date
    Jun 2011
    Posts
    3
    Rep Power
    0

    Default help with jtree icons

    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

    Java Code:
    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 the 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
    Last edited by DarrylBurke; 06-16-2011 at 08:37 AM. Reason: Added code tags

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default

    Class cast exception means you are trying to case one type of object into another which is incompatible - in this case it appears the dmtn user object is a String...are all the userObjects set as Files? I'd recommend posting an SSCCE which clearly demonstrates the problem, which demonstrates how you are setting the user objects (and please use the code tags)

  3. #3
    ankit_k is offline Member
    Join Date
    Jun 2011
    Posts
    3
    Rep Power
    0

    Default

    Quote Originally Posted by doWhile View Post
    Class cast exception means you are trying to case one type of object into another which is incompatible - in this case it appears the dmtn user object is a String...are all the userObjects set as Files? I'd recommend posting an SSCCE which clearly demonstrates the problem, which demonstrates how you are setting the user objects (and please use the code tags)
    as i mentioned i have assigned each node a file with setUserObject() method.after assigning it i have also verified it using getUserObject() method
    i don't know what does SSCCE mean

  4. #4
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default

    Short, Self Contained, Correct Example In other words show us some code that we can run which demonstrates the problem you are having - the shorter the better.

  5. #5
    ankit_k is offline Member
    Join Date
    Jun 2011
    Posts
    3
    Rep Power
    0

    Default

    ok i got the solution,this worked for me:

    Java Code:
    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;    }
    Last edited by ankit_k; 06-17-2011 at 08:08 AM.

Similar Threads

  1. Icons, images, ....
    By kovalensue in forum AWT / Swing
    Replies: 3
    Last Post: 03-18-2011, 12:29 AM
  2. Adding Icons to JTree
    By Nidi in forum New To Java
    Replies: 3
    Last Post: 02-19-2011, 12:23 PM
  3. New Posts Icons
    By desertlizard in forum Suggestions & Feedback
    Replies: 3
    Last Post: 12-06-2010, 05:00 PM
  4. Move JTree item to another JTree.
    By Melki in forum AWT / Swing
    Replies: 8
    Last Post: 07-09-2009, 11:59 AM
  5. is there anyone how can help me with image icons please?
    By xbox_nutter in forum New To Java
    Replies: 4
    Last Post: 03-27-2009, 10:26 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •