Add Icon To JTree Nodes Using The UIManager


/ Published in: Java
Save to your folder(s)

This code snippet uses the UIManager to add icons to JTree nodes.


Copy this code and paste it in your HTML
  1. import java.io.File;
  2. import java.net.URL;
  3. import javax.swing.UIManager;
  4. import javax.swing.JFrame;
  5. import javax.swing.JTree;
  6. import javax.swing.JScrollPane;
  7. import javax.swing.tree.DefaultMutableTreeNode;
  8. import javax.swing.tree.TreePath;
  9. import javax.swing.JOptionPane;
  10. import javax.swing.Icon;
  11. import javax.swing.ImageIcon;
  12.  
  13.  
  14. class Program extends JFrame{
  15.  
  16. public Program(){
  17.  
  18. URL resource = getClass().getResource("NodeImage.jpg");
  19. Icon icon = new ImageIcon(resource);
  20.  
  21. UIManager.put("Tree.closedIcon", icon);
  22. UIManager.put("Tree.openIcon", icon);
  23. UIManager.put("Tree.leafIcon", icon);
  24.  
  25.  
  26.  
  27. root.add(node1);
  28. root.add(node2);
  29. root.add(node3);
  30. root.add(node4);
  31.  
  32. JTree tree = new JTree(root);
  33. JScrollPane treeView = new JScrollPane(tree);
  34.  
  35. this.getContentPane().add(treeView);
  36. }
  37.  
  38. public static void main(String[] args){
  39. Program p = new Program();
  40. p.setSize(300,400);
  41. p.setVisible(true);
  42. }
  43. }

URL: http://www.hostprojects.net/snippets/java/141/add-icon-to-jtree-nodes-using-the-uimanager

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.