我们的还是自定义的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); } }

Jtree (节点的渲染+资源管理器)的更多相关文章

  1. Jtree(节点的渲染+资源管理器)(2)

    上一次我们建立一个比较简单的资源管理器,这次我们说一下上面的资源管理器的问题,并且得尽量的贴近windows的资源管理器. 这样一个简单的资源管理树就完成了,下面我们说说它的问题: ① 图片和外观和W ...

  2. JTree实现电脑资源管理器

    0.前言 最近老师布置了一项用Java写资源管理器的任务,一开始以为简单,但是实际操作起来,却发现网上的资源用起来相对生疏.在使用中,我也遇到了许多问题,虽然不能像其他博主一样,写的非常齐全,但我还是 ...

  3. 使用Windows Form 制作一个简易资源管理器

    自制一个简易资源管理器----TreeView控件 第一步.新建project,进行基本设置:(Set as StartUp Project:View/Toolbox/TreeView) 第二步.开始 ...

  4. HDFS分布式文件系统资源管理器开发总结

      HDFS,全称Hadoop分布式文件系统,作为Hadoop生态技术圈底层的关键技术之一,被设计成适合运行在通用硬件上的分布式文件系统.它和现有的分布式文件系统有很多共同点,但同时,它和其他的分布式 ...

  5. 初识IO流之小型资源管理器

    初次接触到IO流,根据书本上的知识,加上自己的摸索,发现了一些好玩的事情.(书本上的知识或多或少,有时候不足以解决我们的问题!这时候我们就应该自己去求解!!! 所以我们学习的时候要抱有探索的精神,求知 ...

  6. yarn资源管理器高可用性的实现

    资源管理器高可用性 . The ResourceManager (RM) is responsible for tracking the resources in a cluster, and sch ...

  7. windows资源管理器多标签打开 windows文件夹多标签浏览 浏览器tab页面一样浏览文件夹 clover win8 win10 报错 无响应问题怎么解决 clover卡死 clover怎么换皮肤

    大家都知道,我们打开一堆文件夹的时候,是什么样子 “厚厚的一叠”图标堆叠在一起的,非常的不方便 那么,是不是可以像浏览器一样的tab页面展示呢? 答案是可以的 安装好就是这样子的 是不是方便漂亮了很多 ...

  8. winform:简单文件资源管理器

    今天全部学习内容的体现就是winform的资源管理器.这个资源管理器主要由一个textbox获取路径,然后在treeview那里通过递归的方式呈现目录树,当用户点击treeview的节点是,会触发Af ...

  9. Visual C# 2010 实现资源管理器

    演练:使用设计器创建带有 ListView 和 TreeView 控件的资源管理器样式的界面 Visual Studio 2010     其他版本     此主题尚未评级 - 评价此主题   Vis ...

随机推荐

  1. BLOG PLUGINS

    文章分享按钮 (1)加网(JiaThis) (2)百度分享 文章关联推荐 每篇博文下面可以显示你博客中与该篇博文有些关联的几篇文章,也就是智能推荐,一方面可以增加你博文的曝光率和点击率,一方面也可以给 ...

  2. 2013国内IT行业薪资对照表【技术岗】

    (本文为转载,具体出处不详) 说薪水,是所有人最关心的问题.我只 想说如果想在薪水上面满意,在中国,没有哪里比垄断国企好.电力.烟草.通信才是应该努力的方向.但是像我们这种搞研发的进IT行业似乎是注定 ...

  3. [ES6] Module export

    Default export: Default export is easy way to export a function to outside module. //flash-message.j ...

  4. android GestureDetector 手势的判断

    import android.app.Activity;import android.os.Bundle;import android.util.Log;import android.view.Ges ...

  5. c# 操作PPT

    前段时间要做一个把指定图片放到新建的ppt的东西,在网上找了点资料看了一下,发现用C#做好像是最简单的一个,一下是在网上找的一段代码,直接贴进去就能够执行,可是在执行之前一定要加入dll支持:  项目 ...

  6. 大数据笔记09:大数据之Hadoop的HDFS使用

    1. HDFS使用: HDFS内部中提供了Shell接口,所以我们可以以命令行的形式操作HDFS

  7. 监听tableview的点击事件

    // 监听tablview的点击事件 - (void)addAGesutreRecognizerForYourView { UITapGestureRecognizer *tapGesture = [ ...

  8. MySQL 可以用localhost 连接,但不能用IP连接的问题,局域网192.168.*.* 无法连接mysql

    Mysql 默认是没有开启这个权限的(只允许使用 host:localhost,或者 host:127.0.0.1),如果想用 host:192.168.1.* ,来访问mysql ,需要手动开启这个 ...

  9. css选择器基本属性

    选择器一,相邻选择器: 1,相邻选择器 1),定义:相邻选择器匹配指定元素的相邻兄弟元素 2),用法:如果需要选择紧接在另一个元素后的元素,而且二者有相同的父元素,可以使用相邻兄弟选择器 3),表示符 ...

  10. 打印HTML页面部分区域javascript代码

    function preview(oper) { if (oper < 10) { bdhtml = window.document.body.innerHTML; //获取当前页的html代码 ...