Java tutorial
import java.awt.BorderLayout; import java.awt.Component; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JTree; import javax.swing.tree.DefaultMutableTreeNode; import javax.swing.tree.DefaultTreeModel; import javax.swing.tree.TreeCellRenderer; import javax.swing.tree.TreeModel; public class TreeCellRendererImplementation extends JFrame { public TreeCellRendererImplementation() { setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); DefaultMutableTreeNode root = new DefaultMutableTreeNode("+"); root.add(new DefaultMutableTreeNode(new Integer(3))); DefaultMutableTreeNode node = new DefaultMutableTreeNode("*"); node.add(new DefaultMutableTreeNode("string")); node.add(new DefaultMutableTreeNode(new Short((short) 5))); root.add(node); TreeModel tm = new DefaultTreeModel(root); JTree tree = new JTree(tm); tree.setShowsRootHandles(true); tree.setCellRenderer(new MyRenderer()); getContentPane().add(tree, BorderLayout.CENTER); setSize(400, 300); setVisible(true); } private class MyRenderer extends JLabel implements TreeCellRenderer { public Component getTreeCellRendererComponent(JTree tree, Object value, boolean isSelected, boolean isExpanded, boolean isLeaf, int row, boolean hasFocus) { Object userObject = ((DefaultMutableTreeNode) value).getUserObject(); setText(value.toString() + " [" + userObject.getClass().getName() + "]"); return this; } } public static void main(String[] args) { TreeCellRendererImplementation m = new TreeCellRendererImplementation(); } }