Board index » jbuilder » setting JTree icons

setting JTree icons


2003-10-14 12:22:14 AM
jbuilder18
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);
}
}
 
 

Re:setting JTree icons

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.
When I run my sample program I don't get any error messages and I might get
only one out of the three icons displayed. For instance only the
closedFolder icon will display, however, when I expand the node the icon
disappears. It should be swapped with an openFolder icon.
I don't get it! Could someone please look over my code below and advise as
to where I went wrong?
Alan
************************************************
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.net.*;
import javax.swing.tree.*;
public class GetIcon extends JFrame
{
public GetIcon()
{
super("Tree Test Example");
}
public static void main(String[] args)
{
GetIcon myIcon = new GetIcon();
myIcon.init();
}
public void init()
{
setSize(300, 250);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
//Create the nodes.
DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root Node");
root.insert(new DefaultMutableTreeNode("one"), 0);
DefaultMutableTreeNode two = new DefaultMutableTreeNode("two");
two.insert(new DefaultMutableTreeNode("four"), 0);
two.insert(new DefaultMutableTreeNode("five"), 1);
root.insert(two, 1);
root.insert(new DefaultMutableTreeNode("three"), 2);
//Create a tree that allows one selection at a time.
JTree tree = new JTree(root);
DefaultTreeCellRenderer renderer = new DefaultTreeCellRenderer();
renderer.setLeafIcon(new ImageIcon("images/Leaf.gif"));
renderer.setOpenIcon(new ImageIcon("images/OpenFolder.gif"));
renderer.setClosedIcon(new ImageIcon("images/ClosedFolder.gif"));
tree.setCellRenderer(renderer);
tree.setShowsRootHandles(true);
tree.setRowHeight(25);
tree.getSelectionModel().setSelectionMode
(TreeSelectionModel.SINGLE_TREE_SELECTION);
getContentPane().add(tree, BorderLayout.CENTER);
setVisible(true);
}
}
 

Re:setting JTree icons

Code looks reasonable. A quick check - those images are actually in the
relative locations you are referencing, correct?
--
Tad Frysinger [TeamB]
 

{smallsort}