我们的还是自定义的Jtree的类:
package jtree.customNode; import java.io.File; import javax.swing.JTree; import javax.swing.tree.DefaultTreeModel; import util.ImageManager; import jtree.checkBoxTree.CheckBoxTreeNodeListender; public class ZTreeCusNode extends JTree { private static final long serialVersionUID = -581164150235777786L; private static final String ROOT_NAME = "我的电脑"; private static final int levelUp = 3; public ZTreeCusNode() { this.setModel(new DefaultTreeModel(createRootNode())); this.setCellRenderer(new CusNodeTreeCellRender()); this.addCheckSelectListender(); } private void addCheckSelectListender() { this.addMouseListener(new CheckBoxTreeNodeListender(this)); } public CusTreeNode createRootNode(){ CusTreeNode treeNode = null; CusTreeNode rootNode = new CusTreeNode(ROOT_NAME,ImageManager.getImageIconByShortName("home.png")); for(int i = 0; i < File.listRoots().length ; i++){ if(File.listRoots()[i].isDirectory()){ String rootPath = File.listRoots()[i].getPath(); treeNode = creatDefaultMutableTreeNode(rootPath,0); rootNode.add(treeNode); treeNode = null; } } return rootNode; } private CusTreeNode creatDefaultMutableTreeNode(String nodePath,int level) { CusTreeNode node = null; if(level == 0){ node = new CusTreeNode(nodePath,ImageManager.getImageIconByShortName("pan.png")); }else if(level == 1){ node = new CusTreeNode(nodePath,ImageManager.getImageIconByShortName("ff.png")); }else{ node = new CusTreeNode(nodePath); } CusTreeNode treeNode = null; level = level+1; File file = new File(nodePath); if(file.isDirectory() && file.listFiles() != null){ for(int i = 0; i < file.listFiles().length && level < levelUp; i++){ if(file.listFiles()[i].isDirectory()){ String rootPath = file.listFiles()[i].getPath(); treeNode = creatDefaultMutableTreeNode(rootPath,level); node.add(treeNode); treeNode = null; } } } return node; } }
但是因为各个节点的图标,可能比一样,我们把图标的信息集合到节点中去,这样的话,可以根据条件给不同的节点设置不同的节点,显示的时候,直接从节点中取出来即可,根据这种分析,我们需要自定义DefaultMutableTreeNode 。
package jtree.customNode; import javax.swing.Icon; import javax.swing.tree.DefaultMutableTreeNode; import javax.swing.tree.TreeSelectionModel; public class CusTreeNode extends DefaultMutableTreeNode { private static final long serialVersionUID = 3311434895405929201L; private boolean isSelected = false; private boolean enabled = false; private boolean isVisible = false; private Icon icon = null; private String iconName = null; private int selectionMode = 0; public CusTreeNode(Object userObject,Icon icon) { this(userObject, true, false, true, true, icon); } public CusTreeNode(String nodePath) { this(nodePath, true, false, true, true, null); } public CusTreeNode(Object userObject, boolean allowsChildren, boolean isSelected, boolean enabled, boolean isVisible, Icon icon) { super(userObject, allowsChildren); this.isSelected = isSelected; this.enabled = enabled; this.isVisible = isVisible; this.icon = icon; setSelectionMode(TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION); } public boolean isSelected() { return isSelected; } public void setSelected(boolean isSelected) { this.isSelected = isSelected; } public boolean isEnabled() { return enabled; } public void setEnabled(boolean enabled) { this.enabled = enabled; } public boolean isVisible() { return isVisible; } public void setVisible(boolean isVisible) { this.isVisible = isVisible; } public Icon getIcon() { return icon; } public void setIcon(Icon icon) { this.icon = icon; } public String getIconName() { return iconName; } public void setIconName(String iconName) { this.iconName = iconName; } public int getSelectionMode() { return selectionMode; } public void setSelectionMode(int selectionMode) { this.selectionMode = selectionMode; } }
然后是节点的渲染如下:
package jtree.customNode; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Component; import java.awt.Dimension; import javax.swing.Icon; import javax.swing.JPanel; import javax.swing.JTree; import javax.swing.UIManager; import javax.swing.plaf.ColorUIResource; import javax.swing.tree.TreeCellRenderer; import jtree.checkBoxTree.CheckBoxTreeLabel; public class CusNodeTreeCellRender extends JPanel implements TreeCellRenderer { private static final long serialVersionUID = 4676667399191240255L; protected CheckBoxTreeLabel label; // protected JLabel label; public CusNodeTreeCellRender() { setLayout(new BorderLayout()); add(label = new CheckBoxTreeLabel()); // add(label = new JLabel()); label.setForeground(UIManager.getColor("Tree.textForeground")); this.setPreferredSize(new Dimension(100, 20)); } /* * (non-Javadoc) * * @see * javax.swing.tree.TreeCellRenderer#getTreeCellRendererComponent(javax. * swing.JTree, java.lang.Object, boolean, boolean, boolean, int, boolean) */ @Override public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) { String stringValue = tree.convertValueToText(value, selected, expanded, leaf, row, hasFocus); boolean nodeIsEnabled = ((CusTreeNode) value).isEnabled(); setEnabled(nodeIsEnabled); label.setFont(tree.getFont()); label.setText(stringValue); label.setSelected(selected); label.setFocus(hasFocus); if (leaf) label.setIcon(UIManager.getIcon("Tree.leafIcon")); else if (expanded) label.setIcon(UIManager.getIcon("Tree.openIcon")); else{ label.setIcon(UIManager.getIcon("Tree.closedIcon")); } Icon icon = ((CusTreeNode) value).getIcon(); if(icon != null){ label.setIcon(icon); } return this; } @Override public void setBackground(Color color) { if (color instanceof ColorUIResource) color = null; super.setBackground(color); } }
测试代码:
public class TestZTreeCusNode { /** * @param args */ public static void main(String[] args) { ZTreeCusNode tree = new ZTreeCusNode(); JScrollPane scroll = new JScrollPane(tree); //测试的环境 JFrame test = new JFrame(); test.setSize(450, 330); test.setLocation((test.getToolkit().getScreenSize().width - 450) / 2, (test.getToolkit().getScreenSize().height - 330) / 2); test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); test.add(scroll); test.setVisible(true); } }