Bookmark Topic Watch Topic
  • New Topic

setting JTree icons  RSS feed

 
Alan Shiers
Ranch Hand
Posts: 237
  • Mark post as helpful
  • send pies
  • Report post to moderator
Hi there,
I'm working on a sample JTree project where I wanted to render my own icons for open/closed folders and leaf nodes. So I subclassed DefaultTreeCellRenderer and used the inherited ods to set the appropriate icons: setClosedIcon(), setOpenIcon(), and setLeafIcon().
When I run the program I don't get any error messages and the icons don't show either. I don't get it! Could someone look at the code and tell me where I went wrong?
Thanks,
Alan
***********************************************
import java.awt.*;
import javax.swing.*;
import javax.swing.tree.*;
import java.net.*;
public class MyTreeCellRenderer extends DefaultTreeCellRenderer
{
public MyTreeCellRenderer()
{
setFont(new Font("Monospaced", Font.PLAIN, 12));
setHorizontalAlignment(SwingConstants.CENTER);
ImageIcon closed = createImageIcon("/images/ClosedFolder.gif", "ClosedFolder");
if(closed == null)System.out.println("closed is null");
ImageIcon open = createImageIcon("/images/OpenFolder.gif", "OpenFolder");
if(open == null)System.out.println("open is null");
ImageIcon leaf = createImageIcon("/images/Leaf.gif", "Leaf");
if(leaf == null)System.out.println("leaf is null");
setClosedIcon(closed);
setOpenIcon(open);
setLeafIcon(leaf);
}
/** Returns an ImageIcon, or null if the path was invalid. */
protected static ImageIcon createImageIcon(String path, String description)
{
URL imgURL = MyTreeCellRenderer.class.getResource(path);
if (imgURL != null) {
return new ImageIcon(imgURL, description);
} else {
System.err.println("Couldn't find file: " + path);
return null;
}
}
}
************************************************
import java.awt.*;
import java.util.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.tree.*;

public class TestTree extends JFrame
{
JTree tree;
public TestTree()
{
super("Tree Test Example");
setSize(300, 250);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
public static void main(String args[])
{
TestTree tt = new TestTree();
tt.init();
tt.setVisible(true);
}

public void init()
{
//Build the hierarchy of containers & objects
String[] schoolyard = {"School", "Playground", "Parking Lot", "Field"};
String[] mainstreet = {"Grocery", "Shoe Shop", "Five & Dime", "Post Office"};
String[] highway = {"Gas Station", "Convenience Store"};
String[] housing = {"Victorian_Blue", "Faux Colonial", "Victorian_White"};
String[] housing2 = {"Mission", "Ranch", "Condo"};
Hashtable homeHash = new Hashtable();
homeHash.put("Residential 1", housing);
homeHash.put("Residential 2", housing2);
Hashtable cityHash = new Hashtable();
cityHash.put("School grounds", schoolyard);
cityHash.put("Downtown", mainstreet);
cityHash.put("Highway", highway);
cityHash.put("Housing", homeHash);
Hashtable worldHash = new Hashtable();
worldHash.put("My First VRML World", cityHash);
//Build our tree out of our big hashtable
tree = new JTree(worldHash);
tree.setCellRenderer(new MyTreeCellRenderer());
tree.putClientProperty("JTree.lineStyle", "Angled");

getContentPane().add(tree, BorderLayout.CENTER);
}
}
 
Michael Ernest
High Plains Drifter
Sheriff
Posts: 7292
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
  • Report post to moderator
Hi Alan -
This looks like a duplicate of a topic I just moved into the Swing/JFC/AWT forum.
Please do not post multiple versions of one post in the same or different forums. I'm closing this one, and hoping the one I moved will draw the response you're looking for.
Thanks,
 
    Bookmark Topic Watch Topic
  • New Topic
Boost this thread!