Results 1 to 5 of 5
Thread: help with jtree icons
- 06-15-2011, 08:59 PM #1
Member
- Join Date
- Jun 2011
- Posts
- 3
- Rep Power
- 0
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
i have assigned a File object to each node using setUserObject()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'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
thanksLast edited by DarrylBurke; 06-16-2011 at 08:37 AM. Reason: Added code tags
- 06-15-2011, 09:56 PM #2
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,608
- Rep Power
- 5
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)
- 06-16-2011, 07:20 PM #3
Member
- Join Date
- Jun 2011
- Posts
- 3
- Rep Power
- 0
- 06-16-2011, 07:31 PM #4
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,608
- Rep Power
- 5
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.
- 06-16-2011, 08:18 PM #5
Member
- Join Date
- Jun 2011
- Posts
- 3
- Rep Power
- 0
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
-
Icons, images, ....
By kovalensue in forum AWT / SwingReplies: 3Last Post: 03-18-2011, 12:29 AM -
Adding Icons to JTree
By Nidi in forum New To JavaReplies: 3Last Post: 02-19-2011, 12:23 PM -
New Posts Icons
By desertlizard in forum Suggestions & FeedbackReplies: 3Last Post: 12-06-2010, 05:00 PM -
Move JTree item to another JTree.
By Melki in forum AWT / SwingReplies: 8Last Post: 07-09-2009, 11:59 AM -
is there anyone how can help me with image icons please?
By xbox_nutter in forum New To JavaReplies: 4Last Post: 03-27-2009, 10:26 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks