展开和折叠图标

时间:2012-02-01 08:05:33

标签: java swing icons jtree

从下图中可以看出,展开和折叠图标是灰色的,行选择突出显示也是如此。这会导致您在突出显示行时看不到展开或折叠图标(注意:文件夹图标),我想要一个白色展开或折叠图标选中的行。怎么办?

enter image description here enter image description here

在JTree获得焦点之前,将展开和折叠图标完全隐藏起来的其他一些方面也很酷。就像windows 7的树一样。

2 个答案:

答案 0 :(得分:3)

谷歌说 - 根据这篇文章:http://www.exampledepot.com/egs/javax.swing.tree/DefIcons.html - :

// Retrieve the three icons
Icon leafIcon = new ImageIcon("leaf.gif");
Icon openIcon = new ImageIcon("open.gif");
Icon closedIcon = new ImageIcon("closed.gif");

// Create tree
JTree tree = new JTree();

// Update only one tree instance
DefaultTreeCellRenderer renderer = (DefaultTreeCellRenderer)tree.getCellRenderer();
renderer.setLeafIcon(leafIcon);
renderer.setClosedIcon(closedIcon);
renderer.setOpenIcon(openIcon);

// Remove the icons
renderer.setLeafIcon(null);
renderer.setClosedIcon(null);
renderer.setOpenIcon(null);

// Change defaults so that all new tree components will have new icons
UIManager.put("Tree.leafIcon", leafIcon);
UIManager.put("Tree.openIcon", openIcon);
UIManager.put("Tree.closedIcon", closedIcon);

// Create tree with new icons
tree = new JTree();

// Update row height based on new icons;

当然,我不确定你是否可以随时修改图像的颜色。但你可以随时创建新的图标,对吗?

答案 1 :(得分:0)

你可以试试这个。但是你应该注意到要使它工作,我必须覆盖树上的setUI才能允许我的TreeUI。

    private class IconTreeUI extends BasicTreeUI {

    private Icon collapseIcon = null;
    private Icon expandIcon = null;

    @Override
    public Icon getCollapsedIcon() {
        if (collapseIcon == null) {
            collapseIcon = new ImageIcon(yourCollapseImageHere);
        }
        return collapseIcon;
    }

    @Override
    public Icon getExpandedIcon() {
        if (expandIcon == null) {
            expandIcon = new ImageIcon(yourExpandImageHere);
        }
        return expandIcon;
    }}