Search code examples
javaswingiconsjtree

expand and collapse icons


As you can see from the images below the expand and collapse icons are gray, as is the row selection highlight. This causes you to not see the expand or collapse icon (Note: Not the folder icon) when the row is highlighted, I want to have a white expand or collapse icon for the row that is selected. How can that be done?

enter image description here enter image description here

Something else that would also be cool is, to have the expand and collapse icons completely hidden until the JTree gains focus. like windows 7's tree.


Solution

  • Google says -according to this post: 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;
    

    Certainly, I'm not sure if you can modify only the color of the images on-the-go. But you can always create new icons, right?