custom JTree icons

B

Brandon McCombs

I have figured out how to set my own custom icons for my JTree entries
however I want to have a certain icon based on what type of entry it is,
not where it is. Is this possible? I want one icon for the root, another
icon for internal nodes (and I can have different types of internal
nodes that are application specific so I want different icons), and
another type for leaf nodes.

thanks
 
T

Thomas Hawtin

Brandon said:
I have figured out how to set my own custom icons for my JTree entries
however I want to have a certain icon based on what type of entry it is,
not where it is. Is this possible? I want one icon for the root, another
icon for internal nodes (and I can have different types of internal
nodes that are application specific so I want different icons), and
another type for leaf nodes.

It's in the tutorial.

http://java.sun.com/docs/books/tutorial/uiswing/components/tree.html

Tom Hawtin
 
T

Thomas Weidenfeller

Brandon said:
I want one icon for the root, another
icon for internal nodes (and I can have different types of internal
nodes that are application specific so I want different icons), and
another type for leaf nodes.

See the comp.lang.java.gui FAQ.

/Thomas
 
B

Brandon McCombs

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
 
T

Thomas Hawtin

Brandon said:
protected boolean isTutorialBook(Object value) {
DefaultMutableTreeNode node =
(DefaultMutableTreeNode)value;
BookInfo nodeInfo =
(BookInfo)(node.getUserObject());
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 {

The Swing Tutorial code is using the user object (set through the
constructor) to hold the data. No need to extend, let alone override,
setUserObject. You are extending the TreeNode to store additional data.
So switch to either using the user object property throughout, or to
extending TreeNode and ignoring user object.

Tom Hawtin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top