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");
}