java swing中的JTree组件

时间:2010-02-24 16:01:37

标签: swing icons jtree

如何在Swing的JTree组件中更改节点和root的图标?

3 个答案:

答案 0 :(得分:2)

DefaultTreeCellRenderer使用setClosedIconsetOpenIconsetLeafIcon

How to Use Trees复制:

ImageIcon leafIcon = createImageIcon("images/middle.gif");
if (leafIcon != null) {
    DefaultTreeCellRenderer renderer = 
    new DefaultTreeCellRenderer();
    renderer.setLeafIcon(leafIcon);
    tree.setCellRenderer(renderer);
}

答案 1 :(得分:1)

关于JTree的Sun Tutorial有一节介绍如何将TreeCellRenderer子类化以获取JTree中的节点和文本。

答案 2 :(得分:0)

如果您要做的就是为封闭节点,叶子和打开的节点设置不同的图标,那么它非常直接。

//创建3个图标   private Icon customOpenIcon = new ImageIcon(“images / my_open.gif”);   private Icon customClosedIcon = new ImageIcon(“images / my_closed.gif”);   private Icon customLeafIcon = new ImageIcon(“images / my_leaf.gif”);

//假设您创建了DefaultMutableTreeNode层次结构     DefaultMutableTreeNode rootNode = new ...     ...     JTree树=新的JTree(rootNode);

DefaultTreeCellRenderer customRenderer = new DefaultTreeCellRenderer();
customRenderer.setOpenIcon(customOpenIcon);
customRenderer.setClosedIcon(customClosedIcon);
customRenderer.setLeafIcon(customLeafIcon);
tree.setCellRenderer(customRenderer );