Java – dynamically change the icon of a specific node in JTree
•
Java
I've seen many examples of changing node icons during tree instantiation, but I want a way to dynamically change the icons of individual nodes later So in my main code, I add a custom renderer to my tree:
// 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);
My code in the renderer is
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; }
So, obviously, if I set up my renderlicon, it will draw all my nodes with the icons I passed in during tree instantiation I want some way to set the icon of a single node in future program execution
Solution
You can set different icons according to different conditions The following is a simple example of changing the selected node icon:
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,boolean selected,boolean isLeaf,boolean focused) { Component c = super.getTreeCellRendererComponent(tree,isLeaf,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(); } }); } }
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码