Article View: borland.public.jbuilder.java.swing
Article #1572setting JTree icons
From: Alan Shiers
Date: Mon, 13 Oct 2003 16:22
Date: Mon, 13 Oct 2003 16:22
118 lines
4548 bytes
4548 bytes
Hi there,
I'm trying out a sample program using a JTree and I'm trying to render
my own icons for closed/open folders and leaf nodes.
I subclassed DefaultTreeCellRenderer and used the inherited methods
to set the appropriate icons.
When I run my sample program I don't get any error messages and I don't
get any icons being displayed either. I don't get it! Could someone
please look over my code below and advise as to where I went wrong?
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);
}
}
Technical Details
Message-ID:
<3f8ad2ce$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news.maxwell.syr.edu!xmission!news-out.spamkiller.net!propagator2-maxim!news-in.superfeed.net!newsfeed01.tsnz.net!news.xtra.co.nz!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 111d45,5272af97cf26517,start X-Google-Attributes: gid111d45,public X-Google-ArrivalTime: 2003-10-13 09:34:03 PST Date: Mon, 13 Oct 2003 13:22:14 -0300 From: Alan Shiers <ashiers@hfx.eastlink.ca> User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.4) Gecko/20030624 Netscape/7.1 (ax) X-Accept-Language: en-us, en MIME-Version: 1.0 Newsgroups: borland.public.jbuilder.java.swing Subject: setting JTree icons Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit NNTP-Posting-Host: 24.222.101.2 Message-ID: <3f8ad2ce$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1066062542 24.222.101.2 (13 Oct 2003 09:29:02 -0700) Lines: 119 Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news.maxwell.syr.edu!xmission!news-out.spamkiller.net!propagator2-maxim!news-in.superfeed.net!newsfeed01.tsnz.net!news.xtra.co.nz!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail