文档章节

JTree设置,图标,背景色

蒋鑫燚
 蒋鑫燚
发布于 2012/04/02 14:59
字数 1586
阅读 1253
收藏 4

JTree设置,图标,背景色,主程序

package bennkyou.gui.jtree;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;

import javax.swing.Icon;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.TreeCellRenderer;
import javax.swing.tree.TreePath;

import com.shoukaiseki.gui.jinternalframe.MCOCInternalFrame;
import com.shoukaiseki.gui.jtree.IconTreeCellRenderer;
import com.shoukaiseki.gui.tuuyou.StringIcon;
import com.tomaximo.puroguramu.tuuyou.IconSigenn;

/**
 * JTree设置,图标,背景色
 * @author 蒋カイセキ
 *
 */
public class TheJTree extends JInternalFrame {
	public static JFrame frame = new JFrame();
	TheJTree jif = this;

	DefaultMutableTreeNode root;
	JTree jtree;

	public TheJTree() {
		jifInit();
		
		root = new DefaultMutableTreeNode(new StringIcon("root",null));
		
		StringIcon si;

		si=new StringIcon("select",null);
		si.setColor(Color.darkGray, Color.GREEN, Color.yellow, Color.pink);
		si.setToolTipText("select提示字符串");
		si.setTreeRootLeafIcon(IconSigenn.DATABASE, IconSigenn.DBCONN, IconSigenn.SQLFILE);
		DefaultMutableTreeNode child = new DefaultMutableTreeNode(si);

		si.setName("Child");
		si.setColor(Color.red, Color.blue, Color.cyan, Color.magenta);
		si.setToolTipText("提示字符串");
		DefaultMutableTreeNode select = new DefaultMutableTreeNode(si);

		DefaultMutableTreeNode child1 = new DefaultMutableTreeNode(new StringIcon("",null));

		root.add(child);
		root.add(select);
		child.add(child1);

		jtree = new JTree(root);
		add(jtree);
		jtree.setCellRenderer(new IconTreeCellRenderer());
		jtree.repaint();
		jtree.setToolTipText("");//开启
		
//		jtree.setCellRenderer(new DefaultTreeCellRenderer() {
//			public Icon getClosedIcon() {
//				// 返回你需要的图标
//				return Sigenn.DATABASE;
//			}
//
//			public Icon getLeafIcon() {
//				return Sigenn.DATABASE;
//			}
//			public Icon getOpenIcon() {
//				return Sigenn.DATABASE;
//			}
//		});
	}

	public void jifInit() {
		jif.setDefaultCloseOperation(jif.DISPOSE_ON_CLOSE);
		jif.setVisible(true);

	}

	public static void main(String args[]) {
		// JTree
		frameInit();
		frame.getContentPane().add(new TheJTree(), BorderLayout.CENTER);
	}

	public static void frameInit() {

		frame.setSize(700, 500);
		// frame.setLocation(200, 150);
		/**
		 * 设置显示为中心
		 */
		frame.setLocationRelativeTo(null);
		/**
		 * frame.show();失效后采用这个
		 */
		frame.setVisible(true);
		/**
		 * 关闭后终止进程=彻底退出
		 */

		frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
		/**
		 * 最大化 全屏JFrame
		 */
		// frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
		frame.setVisible(true);
	}
}

渲染图标

package com.shoukaiseki.gui.jtree;

import java.awt.Color;
import java.awt.Component;
import java.util.Vector;

import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.TreeCellRenderer;
import javax.swing.tree.TreePath;

import com.shoukaiseki.gui.tuuyou.StringIcon;
import com.tomaximo.puroguramu.tuuyou.IconSigenn;
/**
 * JTree设置,图标,背景色
 * @author 蒋カイセキ
 *
 */
public class IconTreeCellRenderer extends DefaultTreeCellRenderer {
	public static int id = 0;

	public IconTreeCellRenderer() {
		setOpaque(true);
		// setOpenIcon(Sigenn.FOLDEROPEN);
		// setLeafIcon(Sigenn.FILE);
		// setClosedIcon(Sigenn.FOLDERCLOSED);
	}

	@Override
	public Component getTreeCellRendererComponent(JTree tree, Object value,
			boolean selected, boolean expanded, boolean leaf, int row,
			boolean hasFocus) {

		DefaultMutableTreeNode selectNode = (DefaultMutableTreeNode) value;
		
		Object ob = selectNode.getUserObject();
		if (selected) {
			if(((StringIcon) ob).BackgroundSelectionColor == null){
				setBackground(new Color(184,207,229));
			}else{
				setBackground(((StringIcon) ob).BackgroundSelectionColor);
			}

			if(((StringIcon) ob).ForegroundSelectionColor == null){
				setForeground(Color.black);
			}else{
				setBackground(((StringIcon) ob).ForegroundSelectionColor);
			}
			
		} else {
			if(((StringIcon) ob).BackgroundNonSelectionColor == null){
				setBackground(getBackgroundNonSelectionColor());
			}else{
				setBackground(((StringIcon) ob).BackgroundNonSelectionColor);
			}
			if(((StringIcon) ob).ForegroundNonSelectionColor == null){
				setForeground(getTextNonSelectionColor());
			}else{
				setBackground(((StringIcon) ob).ForegroundNonSelectionColor);
			}
		}

//		System.out.println("-------" + ((StringIcon) ob).name);
//		TreePath[] tp = tree.getSelectionPaths();
//		if (tp != null) {
//			for (TreePath treePath : tp) {
//				if (tree.isCollapsed(treePath)) {
//					System.out.println(++id + "hasBeenExpanded");
//				}
//			}
//		}
		setText(((StringIcon) ob).name);
		if (value == null) {
			return this;
		}

		if (((StringIcon) ob).closedIcon == null) {
			setIcon(IconSigenn.FOLDERCLOSED);
		} else {
			setIcon(((StringIcon) ob).closedIcon);
		}
		// TODO 如果 expanded 为 true,则当前扩展该节点,展开后
		if (expanded) {
			if (((StringIcon) ob).openIcon == null) {
				setIcon(IconSigenn.FOLDEROPEN);
			} else {
				setIcon(((StringIcon) ob).openIcon);
			}

		}
		// TODO 如果 leaf 为 true,则该节点表示叶节点
		if (leaf) {
			if (((StringIcon) ob).leafIcon == null) {
				setIcon(IconSigenn.FILE);
			} else {
				setIcon(((StringIcon) ob).leafIcon);
			}
		}
		// TODO 如果 hasFocus 为 true,则该节点当前拥有焦点
		if (hasFocus && leaf) {
			if (((StringIcon) ob).leafIconSelected == null) {
				setIcon(IconSigenn.SELECTED_FILE);
			} else {
				setIcon(((StringIcon) ob).leafIconSelected);
			}
		}
		if (hasFocus && !leaf) {
			if (expanded) {
				if (((StringIcon) ob).openIconSelected == null) {
					setIcon(IconSigenn.FOLDEROPEN_SELECTED);
				} else {
					setIcon(((StringIcon) ob).openIconSelected);
				}
			} else {
				if (((StringIcon) ob).closedIconSelected == null) {
					setIcon(IconSigenn.FOLDERCLOSED_SELECTED);
				} else {
					setIcon(((StringIcon) ob).closedIconSelected);
				}
			}
		}
		if(((StringIcon) ob).getTooleTipText()!=null){
			if(tree.getToolTipText()==null){
				tree.setToolTipText("");
			}
		}
		setToolTipText(((StringIcon) ob).getTooleTipText());
		
		return this;
	}

}

渲染使用的对象

package com.shoukaiseki.gui.tuuyou;

import java.awt.Color;

import javax.swing.ImageIcon;
/**
 * 渲染使用的对象
 * @author 蒋カイセキ
 *
 */
public class StringIcon {
	/**
	 * 唯一ID,比较此值,判断是否为此监听对象
	 */
	private static int idEQU = 0;
	private int ID = 0;
	public String name;
	public String toolTipText;//设置工具提示字符串
	
	public ImageIcon icon;
	public ImageIcon closedIcon;// 用于显示无扩展的非叶节点的图标
	public ImageIcon closedIconSelected;// 父节点关闭时选中图标
	public ImageIcon openIcon;// 用于显示扩展的非叶节点的图标
	public ImageIcon openIconSelected;// 父节点展开后选中图标
	public ImageIcon leafIcon;// 用于显示叶节点的图标
	public ImageIcon leafIconSelected;// 子节点选中图标

	public Color BackgroundSelectionColor;//选中背景色
	public Color ForegroundSelectionColor;//选中前景色
	public Color BackgroundNonSelectionColor;//未选中时背景色
	public Color ForegroundNonSelectionColor;//未选中背景色
	public Object object;//预留
	
	
	public StringIcon(String name, ImageIcon icon) {
		ID = ++idEQU;
		this.name = name;
		this.icon = icon;
	}
	/**
	 * 使用StringIcon(String name, ImageIcon icon)实例化将采用默认的图标
	 * @param name					显示名称
	 * @param closedIcon			用于显示无扩展的非叶节点的图标
	 * @param closedIconselected	父节点关闭时选中图标
	 * @param openIcon				用于显示扩展的非叶节点的图标
	 * @param openIconselected		父节点展开后选中图标
	 * @param leafIcon				用于显示叶节点的图标
	 * @param leafIconselected		子节点选中图标
	 */
	public StringIcon(String name, ImageIcon closedIcon,
			ImageIcon closedIconSelected, ImageIcon openIcon,
			ImageIcon openIconSelected, ImageIcon leafIcon,
			ImageIcon leafIconSelected) {
		ID = ++idEQU;
		this.name = name;
		this.closedIcon = closedIcon;
		this.closedIconSelected = closedIconSelected;
		this.openIcon = openIcon;
		this.openIconSelected = openIconSelected;
		this.leafIcon = leafIcon;
		this.leafIconSelected = leafIconSelected;
	}
	/**
	 * 设置Tree各状态图标
	 * @param closedIcon			用于显示无扩展的非叶节点的图标
	 * @param closedIconselected	父节点关闭时选中图标
	 * @param openIcon				用于显示扩展的非叶节点的图标
	 * @param openIconselected		父节点展开后选中图标
	 * @param leafIcon				用于显示叶节点的图标
	 * @param leafIconselected		子节点选中图标
	 */
	public void setTreeIcon(ImageIcon closedIcon,
			ImageIcon closedIconSelected, ImageIcon openIcon,
			ImageIcon openIconSelected, ImageIcon leafIcon,
			ImageIcon leafIconSelected){
		this.closedIcon = closedIcon;
		this.closedIconSelected = closedIconSelected;
		this.openIcon = openIcon;
		this.openIconSelected = openIconSelected;
		this.leafIcon = leafIcon;
		this.leafIconSelected = leafIconSelected;
	}
	/**
	 * 设置根叶目录的图标,只有这两个状态
	 * @param rootIcon	根目录图标closedIcon,closedIconSelected,openIcon,openIconSelected
	 * @param leafIcon	叶目录图标leafIcon,leafIconSelected
	 */
	public void setTreeRootLeafIcon(ImageIcon rootIcon,
			ImageIcon leafIcon){
		this.closedIcon = rootIcon;
		this.closedIconSelected = rootIcon;
		this.openIcon = rootIcon;
		this.openIconSelected = rootIcon;
		this.leafIcon = leafIcon;
		this.leafIconSelected = leafIcon;
	}
	/**
	 * 设置根叶目录的图标,只有这三个状态
	 * @param rootCloseIcon 根目录折叠时图标closedIcon,closedIconSelected
	 * @param rootOpenIcon 	根目录展开时图标openIcon,openIconSelected
	 * @param leafIcon 		叶目录图标leafIcon,leafIconSelected
	 */
	public void setTreeRootLeafIcon(ImageIcon rootCloseIcon,ImageIcon rootOpenIcon,
			ImageIcon leafIcon){
		this.closedIcon = rootCloseIcon;
		this.closedIconSelected = rootCloseIcon;
		this.openIcon = rootOpenIcon;
		this.openIconSelected = rootOpenIcon;
		this.leafIcon = leafIcon;
		this.leafIconSelected = leafIcon;
	}
		
	/**
	 * 设置颜色
	 * @param BackgroundSelectionColor		//选中背景色
	 * @param ForegroundSelectionColor		//选中前景色
	 * @param BackgroundNonSelectionColor	//未选中时背景色
	 * @param ForegroundNonSelectionColor	//未选中背景色
	 */
	public void setColor(Color BackgroundSelectionColor,Color ForegroundSelectionColor,
			Color BackgroundNonSelectionColor,Color ForegroundNonSelectionColor){
		this.BackgroundSelectionColor=BackgroundSelectionColor;
		this.ForegroundSelectionColor=ForegroundSelectionColor;
		this.BackgroundNonSelectionColor=BackgroundNonSelectionColor;
		this.ForegroundNonSelectionColor=ForegroundNonSelectionColor;
		
	}

	public int getID() {
		return ID;
	}
	public void setName(String name){
		this.name=name;
	}
	/**
	 * 设置预留对象
	 * @param object
	 */
	public void setObject(Object object){
		this.object=object;
	}
	public Object getObject(){
		return this.object;
	}
	/**
	 * 设置工具提示字符串
	 * @param object
	 */
	public void setToolTipText(String text){
		this.toolTipText=text;
	}
	public String getTooleTipText(){
		return this.toolTipText;
	}
}

图标资源 图标源码下载http://www.xlpan.com/home/9367415/90fe9ac1-9a2f-42b2-a609-ea8cb101c816

package com.tomaximo.puroguramu.tuuyou;

import javax.swing.ImageIcon;
/**
 * 图标资源
 * @author 蒋カイセキ
 *
 */
public class IconSigenn {
	public final static String ICONS_PATH="./image/";
	public final static ImageIcon RUN=new ImageIcon(ICONS_PATH+"oracle/javatools/icons/run.png");
	public final static ImageIcon COMMIT=new ImageIcon(ICONS_PATH+"oracle/javatools/icons/commit.png");
	public final static ImageIcon ROLLBACK=new ImageIcon(ICONS_PATH+"oracle/javatools/icons/rollback.png");

	public final static ImageIcon DATABASE=new ImageIcon(ICONS_PATH+"oracle/javatools/icons/database.png");

	public final static ImageIcon DBCONN=new ImageIcon(ICONS_PATH+"oracle/javatools/icons/dbconn.png");
		
	/**
	 * 文件图标
	 */
	public final static ImageIcon FOLDERCLOSED=new ImageIcon(ICONS_PATH+"maximo/folderclosed.gif");
	public final static ImageIcon FOLDEROPEN=new ImageIcon(ICONS_PATH+"maximo/folderopen.gif");
	public final static ImageIcon FOLDER_SELECTED=new ImageIcon(ICONS_PATH+"maximo/folder_selected.gif");

	
	public final static ImageIcon SELECTED_FILE=new ImageIcon(ICONS_PATH+"shoukaiseki/selected_file.png");
	public final static ImageIcon FOLDERCLOSED_SELECTED=new ImageIcon(ICONS_PATH+"shoukaiseki/folderclosed_selected.png");
	public final static ImageIcon FOLDEROPEN_SELECTED=new ImageIcon(ICONS_PATH+"shoukaiseki/folderopen_selected.png");

	
	public final static ImageIcon FILE=new ImageIcon(ICONS_PATH+"oracle/javatools/icons/file.png");
	public final static ImageIcon FOLDER=new ImageIcon(ICONS_PATH+"oracle/javatools/icons/folder.png");
	public final static ImageIcon FOLDERNEW=new ImageIcon(ICONS_PATH+"oracle/javatools/icons/folderNew.png");
	public final static ImageIcon UPFOLDER=new ImageIcon(ICONS_PATH+"oracle/javatools/icons/upFolder.png");
	public final static ImageIcon STATICFOLDER=new ImageIcon(ICONS_PATH+"oracle/javatools/icons/debugger/staticfolder.gif");
	public final static ImageIcon FILESAVE=new ImageIcon(ICONS_PATH+"oracle/javatools/icons/filesave.png");
	public final static ImageIcon REMOVE_FILE=new ImageIcon(ICONS_PATH+"oracle/javatools/icons/remove_file.png");
	public final static ImageIcon SQLFILE=new ImageIcon(ICONS_PATH+"oracle/javatools/icons/sqlfile.png");
	public final static ImageIcon XMLFILE=new ImageIcon(ICONS_PATH+"oracle/javatools/icons/xmlfile.png");
	public final static ImageIcon JAVAFILE=new ImageIcon(ICONS_PATH+"oracle/javatools/icons/javafile.png");
	public final static ImageIcon FILE_UPLOAD=new ImageIcon(ICONS_PATH+"oracle/javatools/icons/file_upload.png");

	public final static ImageIcon FILECONN=new ImageIcon(ICONS_PATH+"oracle/javatools/icons/fileconn.png");
	public final static ImageIcon SIMPLEFILE=new ImageIcon(ICONS_PATH+"oracle/javatools/icons/header/simpleFile.png");

}

© 著作权归作者所有

蒋鑫燚
粉丝 3
博文 12
码字总数 36021
作品 0
台州
私信 提问
加载中

评论(2)

蒋鑫燚
蒋鑫燚 博主

引用来自“Lord超哥”的评论

请问,为什么我在linux环境下编的程序,setBackground(new Color(184,207,229)); 这条语句不起作用?

不知道,我现在没怎么用GUI开发了,在linux下面还是采用命令+xml配置来得简单
Lord超哥
Lord超哥
请问,为什么我在linux环境下编的程序,setBackground(new Color(184,207,229)); 这条语句不起作用?
用JTree子节点触发添加一个JTabbedPane选项卡

用AWT/Swing写了一个签到系统,遇到一个问题百思不得其解啊- - 编写的服务端界面分为三块,左上为一个JTree的面板,左下是一个JTable的面板,右边是一个JEditorPane用来显示客户端和服务端操...

Loblood
2011/09/19
1K
1
开发可编辑的JTree,求指导。。。。

因为项目的要求,需要用java开发一个能够像JTree那样下拉式的树形结构,但是可以批量的删除,树形结构的节点。看到java.swing.Jtree有这样的结构,但是不符合要求,有没有大神给指导一下,应...

竹林草
2013/10/03
148
1
JTree 平行化显示

很早之前就遇到这个问题了一直没能实质上的解决。请各位大神帮个忙。 我想用JTree达到想QQ好友列表一样的效果也就是 Root 我的好友 1…… 2…… 朋友 1…… 2…… 黑名单 1…… 2…… 但JTr...

dh_
2013/07/14
321
2
在开发过程中调试报表插件详细教程

背景说明 目前插件开发调试非常麻烦,需要修改代码,编译出class,重新打插件包。然后删除以前的,安装最新的。过程繁杂,而且不能调试,十分不方便。那么我就来教会大家如何可以调试插件。 ...

九月你好123
2015/07/29
66
0
想请教下java中JTree的用法

想问下java中的JTree如何实现单击不用叶节点时得到不同文本域的内容???

孤火
2015/04/19
182
1

没有更多内容

加载失败,请刷新页面

加载更多

rime设置为默认简体

转载 https://github.com/ModerRAS/ModerRAS.github.io/blob/master/_posts/2018-11-07-rime%E8%AE%BE%E7%BD%AE%E4%B8%BA%E9%BB%98%E8%AE%A4%E7%AE%80%E4%BD%93.md 写在开始 我的Arch Linux上......

zhenruyan
今天
5
0
简述TCP的流量控制与拥塞控制

1. TCP流量控制 流量控制就是让发送方的发送速率不要太快,要让接收方来的及接收。 原理是通过确认报文中窗口字段来控制发送方的发送速率,发送方的发送窗口大小不能超过接收方给出窗口大小。...

鏡花水月
今天
10
0
OSChina 周日乱弹 —— 别问,问就是没空

Osc乱弹歌单(2019)请戳(这里) 【今日歌曲】 @tom_tdhzz :#今日歌曲推荐# 分享容祖儿/彭羚的单曲《心淡》: 《心淡》- 容祖儿/彭羚 手机党少年们想听歌,请使劲儿戳(这里) @wqp0010 :周...

小小编辑
今天
1K
11
golang微服务框架go-micro 入门笔记2.1 micro工具之micro api

micro api micro 功能非常强大,本文将详细阐述micro api 命令行的功能 重要的事情说3次 本文全部代码https://idea.techidea8.com/open/idea.shtml?id=6 本文全部代码https://idea.techidea8....

非正式解决方案
今天
5
0
Spring Context 你真的懂了吗

今天介绍一下大家常见的一个单词 context 应该怎么去理解,正确的理解它有助于我们学习 spring 以及计算机系统中的其他知识。 1. context 是什么 我们经常在编程中见到 context 这个单词,当...

Java知其所以然
昨天
9
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部