Set icon to each node in Jtree

后端 未结 2 1225
既然无缘
既然无缘 2020-12-10 04:34

I want to set for each node in my JTree a different icon, actually I\'m loading each node from a data base, with a \"while\", I set each icon like a root, leaf or parent. Li

2条回答
  •  我在风中等你
    2020-12-10 05:04

    You can use it, a shorter way. "tree" is my JTree component.

    DefaultTreeCellRenderer renderer = (DefaultTreeCellRenderer) tree.getCellRenderer();
    Icon closedIcon = new ImageIcon("closed.png");
    Icon openIcon = new ImageIcon("open.png");
    Icon leafIcon = new ImageIcon("leaf.png");
    renderer.setClosedIcon(closedIcon);
    renderer.setOpenIcon(openIcon);
    renderer.setLeafIcon(leafIcon);    
    

提交回复
热议问题