Customizing Tree.collapsedIcon for a single JTree

空扰寡人 提交于 2019-11-27 05:37:32
trashgod

Another approach is to condition the UIManager when constructing each tree. In the example below, static factories provide the required Icon pair. The example borrows two icons from the MetalLookAndFeel for contrast. Custom icons, seen here, are also possible.

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);
    }
}

If you make the assumption that TreeUI that you are using is an instance of BasicTreeUI, you can do the following:

TreeUI tui = treeInstance.getUI();
if (tui instanceof BasicTreeUI) {
  ((BasicTreeUI)tui).setCollapsedIcon(myIcon);
}

Only way I can see is to create your own version of JTree from the Java source code that allows you to define the icons for each JTree.

I see that JTree gets the icons from javax.accessibility.AccessibleContext and eventually some defined class of the AccessibleIcon interface.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!