动态更改JTree中特定节点的图标

时间:2013-02-19 21:41:29

标签: java swing icons jtree renderer

我已经看到很多在树实例化期间更改节点图标的示例,但我想要一种方法来稍后动态更改单个节点的图标。因此,在我的主代码中,我将自定义渲染器添加到我的树中:

// Icon I want to set nodes to later
ImageIcon checkIcon = new ImageIcon("check.jpg");

// Creates tree with my nodes
JTree tree = new JTree(nodes.top);

// Create custom renderer
Scenario1Renderer renderer = new Scenario1Renderer();

// Set to single tree selection 
tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);

// Set tree to my custom renderer
//renderer.setRendererIcon(greenIcon);
tree.setCellRenderer(renderer);

我在渲染器中的代码是

public class Scenario1Renderer extends DefaultTreeCellRenderer {

ImageIcon rendererIcon;

    public void setRendererIcon(ImageIcon myIcon){
      this.rendererIcon = myIcon;
    };

public Component getTreeCellRendererComponent( 
         JTree tree, 
         Object value, 
         boolean sel, 
         boolean expanded, 
         boolean leaf, 
         int row, 
         boolean hasFocus)
{ 

    Component ret = super.getTreeCellRendererComponent(tree, value,
            selected, expanded, leaf, row, hasFocus);
            //setIcon( rendererIcon ) ;
            return ret;
    } 

所以,显然如果我设置了我的rendererIcon,它将使用我在树实例化期间传入的图标绘制我的所有节点。我想要某种方法,可以在以后的程序执行中设置单个节点的图标。

1 个答案:

答案 0 :(得分:7)

您可以根据不同的条件设置不同的图标。下面是一个更改所选节点图标的简单示例:

import java.awt.Component;
import javax.swing.Icon;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.DefaultTreeModel;

public class TestTree {
    private static void createAndShowUI() {
        JFrame frame = new JFrame();
        final JTree tree = new JTree(buildDemoModel());

        tree.setCellRenderer(new DefaultTreeCellRenderer() {
            private Icon loadIcon = UIManager.getIcon("OptionPane.errorIcon");
            private Icon saveIcon = UIManager.getIcon("OptionPane.informationIcon");
            @Override
            public Component getTreeCellRendererComponent(JTree tree,
                    Object value, boolean selected, boolean expanded,
                    boolean isLeaf, int row, boolean focused) {
                Component c = super.getTreeCellRendererComponent(tree, value,
                        selected, expanded, isLeaf, row, focused);
                if (selected)
                    setIcon(loadIcon);
                else
                    setIcon(saveIcon);
                return c;
            }
        });
        tree.setVisibleRowCount(10);
        frame.add(new JScrollPane(tree));

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationByPlatform(true);
        frame.pack();
        frame.setVisible(true);
    }

    private static DefaultTreeModel buildDemoModel() {
        DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");

        root.add(new DefaultMutableTreeNode("A"));
        root.add(new DefaultMutableTreeNode("B"));
        root.add(new DefaultMutableTreeNode("C"));

        return new DefaultTreeModel(root);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                createAndShowUI();
            }
        });
    }
}