Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Setting the text and icon of a treeNode only displays icon

939631May 24 2012
Hi I'm trying to set the icon and text of treenodes contained in a jtree but only the icons are being displayed on nodes that have both icons and text.

I debugged my code and the node's text is being set properly and read by my IconTreeNodeRenderer but not displayed for some reason.

If anyone knows what the problem might or if there is something else I could do to debug then please let me know.

Here is my code

**IconTreeNode.java**
package com.fmills.gui.components;

import javax.swing.Icon;
import javax.swing.tree.DefaultMutableTreeNode;

public class IconTreeNode extends DefaultMutableTreeNode {

protected Icon icon;

private String text;

public IconTreeNode() {
this(null);
}

public IconTreeNode(Object userObject) {
this(userObject, true, null);
}

public IconTreeNode(Object userObject, boolean allowsChildren, Icon icon) {
super(userObject, allowsChildren);
this.icon = icon;
}

public void setIcon(Icon icon) {
this.icon = icon;
}

public Icon getIcon() {
return icon;
}
public void setText(String text){
this.text=text;
}
public String getText(){
return text;
}

}

IconNodeRenderer.java
package com.fmills.gui.components;

import java.awt.Component;

import javax.swing.Icon;
import javax.swing.JTree;
import javax.swing.tree.DefaultTreeCellRenderer;

public class IconNodeRenderer extends DefaultTreeCellRenderer{

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);

IconTreeNode iconNode = ((IconTreeNode) value);
setText(iconNode.getText());
setIcon(iconNode.getIcon());

return this;
}
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 21 2012
Added on May 24 2012
0 comments
250 views