Thomas said:
I found something similar to that later on after I posted my message but
it doesn't work for me. I'll show you where my problem is. Here is the
tutorial's code for the cell renderer for reference:
private class MyRenderer extends DefaultTreeCellRenderer {
Icon tutorialIcon;
public MyRenderer(Icon icon) {
tutorialIcon = icon;
}
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);
if (leaf && isTutorialBook(value)) {
setIcon(tutorialIcon);
setToolTipText("This book is in the Tutorial series.");
} else {
setToolTipText(null); //no tool tip
}
return this;
}
protected boolean isTutorialBook(Object value) {
DefaultMutableTreeNode node =
(DefaultMutableTreeNode)value;
BookInfo nodeInfo =
(BookInfo)(node.getUserObject());
String title = nodeInfo.bookName;
if (title.indexOf("Tutorial") >= 0) {
return true;
}
return false;
}
}
My code compiles but during runtime I get a class cast exception here:
"BookInfo nodeInfo = (BookInfo)(node.getUserObject());"
The error says I can't convert the String returned from getUserObject()
to, in my case, a Node object so how can this code even work? I looked
at their BookInfo class and they don't override the setUserObject()
method to make it store anything other than a String so how can they get
the string to cast to a BookInfo instance and I can't get my string to
cast to a Node instance? In fact their BookInfo class doesn't even
extend the DefaultMutableTreeNode class so how can they even make the
"value" argument cast to that? It doesn't make sense to me.
Here is my Node class:
public class Node extends DefaultMutableTreeNode {
private static final long serialVersionUID = 0L;
//local DN is needed to more easily keep track of DN paths
private String DN = null;
public Node(NameClassPair entryName, String newDN) {
super(entryName.getName());
DN = newDN;
}
public Node(NameClassPair entryName) {
super(entryName.getName());
DN = entryName.getName();
}
public Node(String entryName) {
super(entryName);
}
public String getDN() {
return DN;
}
public void setDN(String newDN) {
DN = newDN;
}
}
thanks for your help