Newsgroup: it.comp.java
Titolo: JTree con icone personalizzate

E' possibile realizzare un JTree in cui le icone di ogni foglia e di ogni
nodo siano distinte?
Mi spiego meglio:
vorrei visualizzare con un JTree un insieme di file organizzati in cartelle.
Ogni cartella contiene un file .gif che ne rappresenta l'icona e lo stesso
vale per i file (ogni file ha la propria icona).
Le icone dovrebbero essere visualizzate sul JTree.
Esiste qualcosa gia pronto?
Grazie
Gianluca

 wrote in message
997g87$5t9$1...@hermes.unipg.it...


Le icone dovrebbero essere visualizzate sul JTree.
questo e' un esempio che ho spedito qualche giorno fa'
ti permette di avere icone diverse per ogni foglia
ciao_O
//<APPLET code="DemoTree" width="400" height="400"></APPLET>
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.tree.*;
public class DemoTree extends JApplet implements KeyListener, MouseListener, TreeModelListener
{
	private JTree tree;
	private DefaultTreeModel treemodel;
	private CustomTreeCellRenderer treerenderer;
	private CustomMutableTreeNode radice =
	        new CustomMutableTreeNode("radice",null);
	private CustomMutableTreeNode nodoA =
	        new CustomMutableTreeNode("nodo A",null);
	private CustomMutableTreeNode nodoB =
	        new CustomMutableTreeNode("nodo B",new ImageIcon("dog.gif"));
	private CustomMutableTreeNode nodoC =
	        new CustomMutableTreeNode("nodo C",new ImageIcon("home.gif"));
	public void init()
	{
		radice.add(nodoA);
		radice.add(nodoB);
		nodoB.add(nodoC);
		treerenderer = new CustomTreeCellRenderer();
 
		treemodel = new DefaultTreeModel(radice);
		tree = new JTree(treemodel);
		tree.addKeyListener(this);
		tree.addMouseListener(this);
		treemodel.addTreeModelListener(this);
		tree.setCellRenderer(treerenderer);
		getContentPane().add(tree);
	}
	public void keyTyped(KeyEvent e)
	{
	}
	public void keyPressed(KeyEvent e)
	{
	}
	public void keyReleased(KeyEvent e)
	{
		if (e.getKeyCode() == KeyEvent.VK_F5)
		{
			aggiungiNodo(nodoA, "nodo",true);
		}
		if (e.getKeyCode() == KeyEvent.VK_F1)
		{
			treerenderer.setLeafIcon(new ImageIcon("home.gif"));
 
			treemodel.reload();
		}
	}
	public void mouseClicked(MouseEvent e)
	{
	}
	public void mousePressed(MouseEvent e)
	{
		if (e.getModifiers() == InputEvent.BUTTON1_MASK)
		{
			if (e.getComponent() == tree)
			{
				TreePath path = tree.getPathForLocation(e.getX(), e.getY());
				if (path != null)
				{
					System.out.println(path);
//					TreeNode node =
//		(DefaultMutableTreeNode) path.getLastPathComponent();
//					System.out.println(node);
				}
			}
		}
	}
	public void mouseReleased(MouseEvent e)
	{
	}
	public void mouseEntered(MouseEvent e)
	{
	}
	public void mouseExited(MouseEvent e)
	{
	}
	public DefaultMutableTreeNode aggiungiNodo(
	       CustomMutableTreeNode parent, String child, boolean visible)
	{
		if (parent == null)
		{
			parent = radice;
		}
		DefaultMutableTreeNode node = 
		      new DefaultMutableTreeNode(child + parent.getChildCount());
		treemodel.insertNodeInto(node, parent, parent.getChildCount());
		if (visible)
		{
			tree.makeVisible(new TreePath(node.getPath()));
		}
		return (node);
	}
	public void treeNodesChanged(TreeModelEvent e)
	{
		System.out.println(e);
	}
	public void treeNodesInserted(TreeModelEvent e)
	{
		System.out.println(e);
	}
	public void treeNodesRemoved(TreeModelEvent e)
	{
		System.out.println(e);
	}
	public void treeStructureChanged(TreeModelEvent e)
	{
		System.out.println(e);
	}
	private class CustomMutableTreeNode extends DefaultMutableTreeNode
	{
		private Icon icon;
		public CustomMutableTreeNode(Object o, Icon i)
		{
			super(o);
			icon = i;
		}
		public Icon getIcon()
		{
			return (icon);
		}
	}
	public class CustomTreeCellRenderer extends DefaultTreeCellRenderer
	{
		public CustomTreeCellRenderer()
		{
		}
		public Component getTreeCellRendererComponent(JTree tree,
		        Object value, boolean selected, boolean expanded,
		        boolean leaf, int row, boolean hasFocus)
		{
			String stringValue =
	        tree.convertValueToText(value, selected, expanded, leaf, row, hasFocus);
			this.hasFocus = hasFocus;
			setText(stringValue);
			if (selected)
			{
				setForeground(getTextSelectionColor());
			}
			else
			{
				setForeground(getTextNonSelectionColor());
			}
			if (!tree.isEnabled())
			{
				setEnabled(false);
				if (leaf)
				{
					setDisabledIcon(getLeafIcon());
				}
				else if (expanded)
				{
					setDisabledIcon(getOpenIcon());
				}
				else
				{
					setDisabledIcon(getClosedIcon());
				}
			}
			else
			{
				setEnabled(true);
				Icon i = null;
 
				if (value instanceof CustomMutableTreeNode)
				{
					i = ((CustomMutableTreeNode) value).getIcon();
				}
				if (i != null)
				{
					setIcon(i);
				}
				else if (leaf)
				{
					setIcon(getLeafIcon());
				}
				else if (expanded)
				{
					setIcon(getOpenIcon());
				}
				else
				{
					setIcon(getClosedIcon());
				}
			}
			setComponentOrientation(tree.getComponentOrientation());
			this.selected = selected;
			return (this);
		}
	}
}
http://digilander.iol.it/schiele