要重写DefaultTreeCellRenderer,又要不失去它本身的功能,唯一的办法就是继承它,拓展它的方法,不仅要继承该类,我们还要继承DefaultMutableTreeNode类,使我们的节点更加丰富
下面笔者将创建类IconNodeRenderer继承类DefaultTreeCellRenderer,
创建类IconNode继承DefaultMutableTreeNode
为了更方便的使用我们自定义的树,笔者将这两个类封装在一个文件里,便于管理
代码如下:
import java.awt.Component;
import javax.swing.Icon;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
public class IconNodeRenderer extends DefaultTreeCellRenderer//继承该类
{
//重写该方法
public Component getTreeCellRendererComponent(JTree tree, Object value,boolean sel, boolean expanded, boolean leaf, int row,boolean hasFocus)
{
super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf,row, hasFocus); //调用父类的该方法
Icon icon = ((IconNode) value).getIcon();//从节点读取图片
String txt=((IconNode) value).getText(); //从节点读取文本
setIcon(icon);//设置图片
setText(txt);//设置文本
return this;
}
}
//定义节点类
class IconNode extends DefaultMutableTreeNode
{
protected Icon icon;
protected String txt;
//只包含文本的节点构造
public IconNode(String txt)
{
super();
this.txt=txt;
}
//包含文本和图片的节点构造
public IconNode(Icon icon,String txt)
{
super();
this.icon = icon;
this.txt = txt;
}
public void setIcon(Icon icon)
{
this.icon = icon;
}
public Icon getIcon()
{
return icon;
}
public void setText(String txt)
{
this.txt=txt;
}
public String getText()
{
return txt;
}
}
现在我们写一个测试函数来看看我们定义的树,顺便看看如何使用
一样得先准备及张16x16的小图片
代码如下:
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;
//import com.jtattoo.plaf.bernstein.BernsteinLookAndFeel;
public class text
{
JFrame frame;
public text()
{
try
{
// UIManager.setLookAndFeel("com.jtattoo.plaf.bernstein.BernsteinLookAndFeel");
SwingUtilities.updateComponentTreeUI(frame);
}catch(Exception e){}
frame=new JFrame("树");
frame.setSize(150,300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
IconNode root1=new IconNode(new ImageIcon("3.png"),"高中同学");
IconNode root2=new IconNode(new ImageIcon("3.png"),"初中同学");
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();
}
}
看看,我们设置了不同的头像了,而且我们拓展了DefaultTreeCellRenderer所没有的方法
setText(),getText(),setIcon(),getIcon()等四个方法来修改节点的信息。