我知道您可以在Swing中使用Tree.collapsedIcon更改应用程序中所有JTreesUImanager。例如:

UIManager.put("Tree.collapsedIcon",closedcabinet);

我想要在同一应用程序中为单个Tree.collapsedIcon更改JTrees的灵活性,最终结果是,对于同一应用程序中的不同树,Tree.collpasedIcon可能会以不同的方式出现。

我知道如何使用自定义渲染器自定义单个图标。例如,我使用setIcon设置叶子的图标,使用SetOpenIcon设置具有扩展子节点的节点的图标,而SetCloseIcon设置没有叶子的节点的图标。

但是除了使用具有上述局限性的Tree.collapsedIcon之外,我看不到如何针对UIManager进行此操作。

有人知道怎么做吗?

最佳答案

另一种方法是在构造每棵树时调节UIManager。在下面的示例中,静态工厂提供所需的Icon对。该示例从MetalLookAndFeel借用了两个图标进行对比。也可以使用here所示的自定义图标。

frame.add(new JTreePanel(Icons.getDefault()));
frame.add(new JTreePanel(Icons.getMetal()));

SSCCE:
import java.awt.EventQueue;
import java.awt.GridLayout;
import javax.swing.Icon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTree;
import javax.swing.LookAndFeel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.plaf.metal.MetalLookAndFeel;

/** @see https://stackoverflow.com/a/14262706/230513 */
public class JTreePanel extends JPanel {

    public static final class Icons {

        private static final String COLLAPSED_ICON = "Tree.collapsedIcon";
        private static final String EXPANDED_ICON = "Tree.expandedIcon";
        private Icon collapsed;
        private Icon expanded;

        public static Icons getDefault() {
            Icons icons = new Icons();
            icons.collapsed = (Icon) UIManager.get(COLLAPSED_ICON);
            icons.expanded = (Icon) UIManager.get(EXPANDED_ICON);
            return new Icons();
        }

        public static Icons getMetal() {
            Icons icons = new Icons();
            try {
                LookAndFeel save = UIManager.getLookAndFeel();
                LookAndFeel laf = new MetalLookAndFeel();
                UIManager.setLookAndFeel(laf);
                icons.collapsed = (Icon) UIManager.get(COLLAPSED_ICON);
                icons.expanded = (Icon) UIManager.get(EXPANDED_ICON);
                UIManager.setLookAndFeel(save);
            } catch (UnsupportedLookAndFeelException ex) {
                ex.printStackTrace(System.err);
            }
            return icons;
        }
    }

    public JTreePanel(Icons pair) {
        UIManager.put("Tree.collapsedIcon", pair.collapsed);
        UIManager.put("Tree.expandedIcon", pair.expanded);
        JTree tree = new JTree();
        this.add(tree);
        for (int i = 0; i < tree.getRowCount(); i++) {
            tree.expandRow(i);
        }
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                createGUI();
            }
        });
    }

    private static void createGUI() {
        final JFrame frame = new JFrame();
        frame.setLayout(new GridLayout(1, 0));
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new JTreePanel(Icons.getDefault()));
        frame.add(new JTreePanel(Icons.getMetal()));
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}

10-04 10:44