概述
public class ToolTipNode extends JFrame implements TreeCellRenderer{
private static final long serialVersionUID=-1884123037630846839L;
private DefaultTreeCellRenderer renderer=new DefaultTreeCellRenderer();
private Map<DefaultMutableTreeNode,String>map;//保存键值树
public ToolTipNode(Map<DefaultMutableTreeNode,String>map){//初始化键值树
this.map=map;
}
public Component getTreeCellRendererComponent(JTree tree,Object value,boolean selected,boolean expanded,boolean leaf,int row,boolean hasFocus){
//调用默认的方法
renderer.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);
renderer.setToolTipText("<html><font face=微软雅黑 size=16 color=red>"+map.get(value)+"</font></html>");
return renderer;
}
public ToolTipNode(){
addWindowListener(new WindowListener(){
public void windowActivated(WindowEvent e){
do_this_windowActivated(e);
}
@Override
public void windowClosed(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowClosing(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowDeactivated(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowDeiconified(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowIconified(WindowEvent e) {
// TODO Auto-generated method stub
}
@Override
public void windowOpened(WindowEvent e) {
// TODO Auto-generated method stub
}
});
setSize(300,300);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new ToolTipNode();
}
public void do_this_windowActivated(WindowEvent e){
DefaultMutableTreeNode root=new DefaultMutableTreeNode("明日科技新书");//创建根节点
DefaultMutableTreeNode parent1=new DefaultMutableTreeNode("从入门到精通系列");
parent1.add(new DefaultMutableTreeNode("Java从入门到精通"));
parent1.add(new DefaultMutableTreeNode("PHP从入门到精通"));
parent1.add(new DefaultMutableTreeNode("VB从入门到精通"));
root.add(parent1);
DefaultMutableTreeNode parent2=new DefaultMutableTreeNode("编程词典系列");
parent2.add(new DefaultMutableTreeNode("Java编程词典"));
parent2.add(new DefaultMutableTreeNode("PHP编程词典"));
parent2.add(new DefaultMutableTreeNode("VB编程词典"));
root.add(parent2);
JTree tree=new JTree(root);
DefaultTreeModel model=new DefaultTreeModel(root);//使用根节点创建树模型
tree.setModel(model);
/*ToolTipManager.sharedInstance().registerComponent(tree);//为树注册提示信息
//利用映射保存提示信息
Map<DefaultMutableTreeNode,String>map=new HashMap<DefaultMutableTreeNode,String>();
map.put(root, "明日科技");
map.put(parent1, "明日科技");
map.put(parent2, "明日科技");
tree.setCellRenderer(new ToolTipNode(map));//设置新的渲染起*/
JTextField textField=new JTextField();//创建文本域对象
textField.setFont(new Font("微软雅黑",Font.PLAIN, 16));
TreeCellEditor editor=new DefaultCellEditor(textField);//创建树编辑器
tree.setEditable(true);
tree.setCellEditor(editor);//使用树编辑器
getContentPane().add(tree, BorderLayout.CENTER);
}
}
最后
以上就是执着楼房为你收集整理的双击编辑树节点功能的全部内容,希望文章能够帮你解决双击编辑树节点功能所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复