标签:
1 package JTree; 2 3 import java.awt.Component; 4 import javax.swing.Icon; 5 import javax.swing.JTree; 6 import javax.swing.tree.DefaultMutableTreeNode; 7 import javax.swing.tree.DefaultTreeCellRenderer; 8 9 10 public class IconNodeRenderer extends DefaultTreeCellRenderer { //继承该类 11 //重写该方法 12 public Component getTreeCellRendererComponent(JTree tree, Object value,boolean sel, 13 boolean expanded, boolean leaf, int row,boolean hasFocus){ 14 super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf,row, hasFocus); //调用父类的该方法 15 Icon icon = ((IconNode) value).getIcon();//从节点读取图片 16 String txt=((IconNode) value).getText(); //从节点读取文本 17 setIcon(icon);//设置图片 18 setText(txt);//设置文本 19 return this; 20 } 21 } 22 23 class IconNode extends DefaultMutableTreeNode { 24 protected Icon icon; 25 protected String txt; 26 27 //只包含文本的节点构造 28 public IconNode(String txt) 29 { 30 super(); 31 this.txt=txt; 32 } 33 34 //包含文本和图片的节点构造 35 public IconNode(Icon icon,String txt) 36 { 37 super(); 38 this.icon = icon; 39 this.txt = txt; 40 } 41 public void setIcon(Icon icon) 42 { 43 this.icon = icon; 44 } 45 46 public Icon getIcon() 47 { 48 return icon; 49 } 50 51 public void setText(String txt) 52 { 53 this.txt=txt; 54 } 55 56 public String getText() 57 { 58 return txt; 59 } 60 }
package JTree; import java.awt.BorderLayout; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.JTree; import javax.swing.SwingUtilities; import javax.swing.UIManager; import javax.swing.tree.DefaultTreeCellRenderer; import javax.swing.tree.TreePath; public class text { JFrame frame; public text() { frame=new JFrame("树"); frame.setSize(150,300); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //java.net.URL imgURL=getClass().getResource(""); IconNode root1=new IconNode(new ImageIcon("images/3.jpg"),"高中同学"); IconNode root2=new IconNode(new ImageIcon("i"),"初中同学"); root1.add(new IconNode(new ImageIcon("5.png"),"雅君")); root1.add(new IconNode(new ImageIcon("1.png"),"伟旭")); root1.add(new IconNode(new ImageIcon("2.png"),"宜群")); root2.add(new IconNode(new ImageIcon("2.png"),"彬强")); root2.add(new IconNode(new ImageIcon("1.png"),"小强")); IconNode Root=new IconNode(null,null);//定义根节点 Root.add(root1);//定义二级节点 Root.add(root2);//定义二级节点 final JTree tree = new JTree(Root);//定义树 tree.setCellRenderer(new IconNodeRenderer()); //设置单元格描述 tree.setEditable(false); //设置树是否可编辑 tree.setRootVisible(false);//设置树的根节点是否可视 tree.setToggleClickCount(1);//设置单击几次展开数节点 DefaultTreeCellRenderer cellRenderer=(DefaultTreeCellRenderer)tree.getCellRenderer();//获取该树的Renderer cellRenderer.setClosedIcon(new ImageIcon("2.gif"));//关闭打开图标 cellRenderer.setOpenIcon(new ImageIcon("2.gif"));//设置展开图标 //测试事件 tree.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { if(e.getClickCount()==2)//双击节点 { TreePath path=tree.getSelectionPath();//获取选中节点路径 IconNode node=(IconNode)path.getLastPathComponent();//通过路径将指针指向该节点 if(node.isLeaf())//如果该节点是叶子节点 { //DefaultTreeModel model=(DefaultTreeModel)tree.getModel();//获取该树的模型 //model.removeNodeFromParent(node);//从本树删除该节点 node.setIcon(new ImageIcon("3.png"));//修改该节点的图片 node.setText("双击");//修改该节点的文本 tree.repaint();//重绘更新树 System.out.println(node.getText()); } else//不是叶子节点 { } } } }); JScrollPane sp = new JScrollPane(tree); frame.getContentPane().add(sp, BorderLayout.CENTER); frame.setVisible(true); } public static void main(String[] args) { new text(); } }
标签:
原文地址:http://www.cnblogs.com/tannian/p/4316974.html