• Post Reply Bookmark Topic Watch Topic
  • New Topic

extending DefaultTreeCellRenderer class...help me  RSS feed

 
NIKHIL EROS
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i m displaying my tree by extending DefaultTreeCellRenderer..
and using my own class called FriendInfo for the construction of DefaultMutableTreeNode for the tree....
private class MyRenderer extends DefaultTreeCellRenderer {
ImageIcon availableIcon;
ImageIcon offlineIcon;
ImageIcon awayIcon;
ImageIcon awayPendingIcon;
public MyRenderer() {
availableIcon = new ImageIcon("images/Available.gif");
offlineIcon = new ImageIcon("images/Offline.gif");
awayIcon = new ImageIcon("images/Away.gif");
awayPendingIcon = new ImageIcon("images/AwayPending.gif");
}
public Component getTreeCellRendererComponent(
JTree tree,
Object value,
boolean sel,
boolean expanded,
boolean leaf,
int row,
boolean hasFocus) {
super.getTreeCellRendererComponent(
tree, value, sel,
expanded, leaf, row,
hasFocus);
int holdStatus = whatStatus(value);
if (leaf && (holdStatus == 1)) {
setIcon(availableIcon);
setToolTipText("This friend is online");
}
if (leaf && (holdStatus == 2)) {
setIcon(offlineIcon);
setToolTipText("This friend is offline");
}
if (leaf && (holdStatus == 3)) {
setIcon(awayIcon);
setToolTipText("This friend is away");
}
if (leaf && (holdStatus == 4)) {
setIcon(awayPendingIcon);
setToolTipText("This friend is away pending");
}

return this;
}
protected int whatStatus(Object value) {
DefaultMutableTreeNode node =
(DefaultMutableTreeNode)value;
FriendInfo nodeInfo =
(FriendInfo)(node.getUserObject());//<--error
//System.out.println(nodeInfo.toString());
String status = nodeInfo.status;
if(status.equals("Available")){
return 1;
}
if(status.equals("Offline")){
return 2;
}
if(status.equals("Away")){
return 3;
}
if(status.equals("AwayPending")){
return 4;
}
return 0;
}
}
}
//------------------------------------------------------------------------
class FriendInfo {
public String friendName;
public String status;
public FriendInfo(String friendName, String status) {
this.friendName = friendName;
this.status = status;
}
public String toString() {
return (friendName + " (" + status + ")");
}
}
//---------------------------------------------------------------
at run time it is giving "ClassCastException: Java.lang.String" at the line marked above in the code in whatStatus() method of my MyRenderer class...
i have wasted several hours of mine in getting help from books and java sun site, but i could not solve the problem..
please help me..
 
Thomas Suer
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please try out these sites:
http://www2.gol.com/users/tame/swing/examples/JTreeExamples1.html http://www2.gol.com/users/tame/swing/examples/JTreeExamples2.html
In my mind you don't have a chance using the DefaultTreeCellRenderer, but maybe I'm wrong...
Good luck!
Tom
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!