JTree,将树的每个节点设置成不同的图标

http://xieruilin.iteye.com/blog/717858

 

MyDefaultTreeCellRenderer.java

Java代码 收藏代码

  1. import java.awt.Component; 
  2. import javax.swing.ImageIcon; 
  3. import javax.swing.JTree; 
  4. import javax.swing.tree.DefaultMutableTreeNode; 
  5. import javax.swing.tree.DefaultTreeCellRenderer; 
  6. /**
  7. * 自定义树描述类,将树的每个节点设置成不同的图标
  8. * @author RuiLin.Xie - xKF24276
  9. *
  10. */
  11. public class MyDefaultTreeCellRenderer extends DefaultTreeCellRenderer 
  12. /**
  13.      * ID
  14.      */
  15. private static final long   serialVersionUID    = 1L; 
  16. /**
  17.      * 重写父类DefaultTreeCellRenderer的方法
  18.      */
  19. @Override
  20. public Component getTreeCellRendererComponent(JTree tree, Object value, 
  21. boolean sel, boolean expanded, boolean leaf, int row, 
  22. boolean hasFocus) 
  23.     { 
  24. //执行父类原型操作
  25. super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, 
  26.                 row, hasFocus); 
  27.         setText(value.toString()); 
  28. if (sel) 
  29.         { 
  30.             setForeground(getTextSelectionColor()); 
  31.         } 
  32. else
  33.         { 
  34.             setForeground(getTextNonSelectionColor()); 
  35.         } 
  36. //得到每个节点的TreeNode
  37.         DefaultMutableTreeNode node = (DefaultMutableTreeNode) value; 
  38. //得到每个节点的text
  39.         String str = node.toString();        
  40. //判断是哪个文本的节点设置对应的值(这里如果节点传入的是一个实体,则可以根据实体里面的一个类型属性来显示对应的图标)
  41. if (str == "a") 
  42.         { 
  43. this.setIcon(new ImageIcon("treeimg/a.GIF")); 
  44.         } 
  45. if (str == "b") 
  46.         { 
  47. this.setIcon(new ImageIcon("treeimg/b.GIF")); 
  48.         } 
  49. if (str == "c") 
  50.         { 
  51. this.setIcon(new ImageIcon("treeimg/c.GIF")); 
  52.         } 
  53. return this; 
  54.     } 

TreeIconDemo.java

Java代码 收藏代码

  1. import java.awt.BorderLayout; 
  2. import javax.swing.JFrame; 
  3. import javax.swing.JScrollPane; 
  4. import javax.swing.JTree; 
  5. import javax.swing.tree.DefaultMutableTreeNode; 
  6. import javax.swing.tree.DefaultTreeModel; 
  7. public class TreeIconDemo extends JFrame 
  8. /**
  9.      * ID
  10.      */
  11. private static final long   serialVersionUID    = 1L; 
  12. private JTree   tree; 
  13. /**
  14.      * Launch the application
  15.      *
  16.      * @param args
  17.      */
  18. public static void main(String args[]) 
  19.     { 
  20. try
  21.         { 
  22.             TreeIconDemo frame = new TreeIconDemo(); 
  23.             frame.setVisible(true); 
  24.         } 
  25. catch (Exception e) 
  26.         { 
  27.             e.printStackTrace(); 
  28.         } 
  29.     } 
  30. /**
  31.      * Create the frame
  32.      */
  33. public TreeIconDemo() 
  34.     { 
  35. super(); 
  36.         setBounds(100, 100, 500, 375); 
  37.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
  38. final JScrollPane scrollPane = new JScrollPane(); 
  39.         getContentPane().add(scrollPane, BorderLayout.CENTER); 
  40.         tree = new JTree(); 
  41.         scrollPane.setViewportView(tree); 
  42. //创建数据
  43.         DefaultTreeModel defaultTreeModel = createModel(); 
  44. //设置数据
  45.         tree.setModel(defaultTreeModel); 
  46. //设置自定义描述类
  47.         tree.setCellRenderer(new MyDefaultTreeCellRenderer()); 
  48.     } 
  49. /**
  50.      * 创建树节点模型
  51.      * @return
  52.      */
  53. public DefaultTreeModel createModel() 
  54.     { 
  55.         DefaultMutableTreeNode root = new DefaultMutableTreeNode("a"); 
  56.         DefaultMutableTreeNode b = new DefaultMutableTreeNode("b"); 
  57.         DefaultMutableTreeNode c = new DefaultMutableTreeNode("c"); 
  58.         root.add(b); 
  59.         root.add(c); 
  60. return new DefaultTreeModel(root); 
  61.     } 

}

你可能感兴趣的:(JTree)