Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Swing / AWT / SWT
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Tim Cooke
Campbell Ritchie
Ron McLeod
Junilu Lacar
Liutauras Vilda
Sheriffs:
Paul Clapham
Jeanne Boyarsky
Henry Wong
Saloon Keepers:
Tim Moores
Tim Holloway
Stephan van Hulst
Piet Souris
Carey Brown
Bartenders:
Jesse Duncan
Frits Walraven
Mikalai Zaikin
Forum:
Swing / AWT / SWT
Help with Jtree
shri mon
Ranch Hand
Posts: 63
posted 17 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Is it possible to set two different icons for two different leaves in a JTree.
Thanks in advance.
Shrimon
Tad Dicks
Ranch Hand
Posts: 264
posted 17 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Yes. You need to extend/implement your own TreeCellRenderer.
Craig Wood
Ranch Hand
Posts: 1535
posted 17 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
import java.awt.*; import java.awt.image.BufferedImage; import java.io.*; import java.net.*; import javax.imageio.ImageIO; import javax.swing.*; import javax.swing.tree.*; public class TreeIcons { public TreeIcons() { JTree tree = getTree(getIcons()); tree.setCellRenderer(new CritterRenderer()); JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.getContentPane().add(new JScrollPane(tree)); f.setSize(400,400); f.setLocation(200,200); f.setVisible(true); } private JTree getTree(ImageIcon[] icons) { String[] critters = { "bird", "rabbit" }; int[] indices = { 0, 1, 1, 0, 1, 0, 0, 1 }; DefaultMutableTreeNode root = new DefaultMutableTreeNode( new CritterNode("birds and rabbits", null)); for(int j = 0; j < 2; j++) { DefaultMutableTreeNode node = new DefaultMutableTreeNode( new CritterNode("set " + (j+1), null)); root.add(node); for(int k = 0; k < indices.length/2; k++) { int index = indices[j*indices.length/2 + k]; String s = critters[index]; DefaultMutableTreeNode leaf = new DefaultMutableTreeNode( new CritterNode(s, icons[index])); node.add(leaf); } } return new JTree(new DefaultTreeModel(root)); } private ImageIcon[] getIcons() { String[] fileNames = { "Bird.gif", "Rabbit.gif" }; ImageIcon[] icons = new ImageIcon[fileNames.length]; for(int j = 0; j < icons.length; j++) try { URL url = getClass().getResource("images/" + fileNames[j]); icons[j] = new ImageIcon(ImageIO.read(url)); } catch(MalformedURLException mue) { System.err.println("url: " + mue.getMessage()); } catch(IOException ioe) { System.err.println("read: " + ioe.getMessage()); } return icons; } public static void main(String[] args) { new TreeIcons(); } } class CritterRenderer implements TreeCellRenderer { JLabel label; public CritterRenderer() { label = new JLabel(); } public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) { DefaultMutableTreeNode node = (DefaultMutableTreeNode)value; CritterNode cn = (CritterNode)node.getUserObject(); label.setText(cn.text); label.setIcon(cn.icon); return label; } } class CritterNode { String text; ImageIcon icon; public CritterNode(String s, ImageIcon icon) { text = s; this.icon = icon; } }
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Help with Jtree
JTree
JTree flicker
JTree and XML
JTree
More...