Mettre une image dans un jtree

_stic_ Messages postés 11 Date d'inscription samedi 22 octobre 2005 Statut Membre Dernière intervention 19 novembre 2009 - 24 mai 2007 à 10:22
cs_AlexN Messages postés 694 Date d'inscription lundi 5 décembre 2005 Statut Membre Dernière intervention 8 janvier 2014 - 24 mai 2007 à 11:57
Bonjour, j'aimerais mettre une image à la place de l'image du dossier dans un jtree à chaque noeud. Est ce que quelqu'un sait comment faire ?

Merci

2 réponses

cs_AlexN Messages postés 694 Date d'inscription lundi 5 décembre 2005 Statut Membre Dernière intervention 8 janvier 2014 18
24 mai 2007 à 11:37
il faut utiliser un TreeCellRenderer
0
cs_AlexN Messages postés 694 Date d'inscription lundi 5 décembre 2005 Statut Membre Dernière intervention 8 janvier 2014 18
24 mai 2007 à 11:57
plus exactement comme ça :

tu définis une classe qui dérive de DefaultTreeCellRenderer :

import java.awt.Component;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JTree;
import javax.swing.tree.DefaultTreeCellRenderer;

public class MyTreeCellRenderer extends DefaultTreeCellRenderer {
  /**
   * icon used to display on the tree
   */   
  public static Icon icon= null;
  /** Creates a new instance of MyTreeCellRenderer */
  public MyTreeCellRenderer() {     
    icon = new ImageIcon(getClass().getResource("MyIcon.gif"));
  }
  /**
   * Sets the value of the current tree cell to value.
   * If selected is true, the cell will be drawn as if selected.
   * If expanded is true the node is currently expanded and if leaf is
   * true the node represets a leaf and if hasFocus is true the node
   * currently has focus. tree is the JTree the receiver is being
   * configured for. Returns the Component that the renderer uses to draw the value.
   *
   * Modified the TreeCellRender to match a specific nodes attributes. If
   * the node is a match, then blue is used for the text color.  Icon is customized
   *
   * @param tree the JTree being redrawn
   * @param value node in the tree
   * @param sel true if selected by the user
   * @param expanded true if path is expanded
   * @param leaf true if this node is a leaf
   * @param row row number (vertical position)
   * @param hasFocus true if it has the focus
   * @return the Component that the renderer uses to draw the value

   */  
  public Component getTreeCellRendererComponent(
                      JTree tree,
                      Object value,
                      boolean sel,
                      boolean expanded,
                      boolean leaf,
                      int row,
                      boolean hasFocus) {

      super.getTreeCellRendererComponent(
                      tree, value, sel,
                      expanded, leaf, row,
                      hasFocus);
     
      if(icon != null)    // set a custom icon
      {
          setOpenIcon(icon);
          setClosedIcon(icon);
          setLeafIcon(icon);
      }
      return this;
  }
}

puis lors de la définition du Jtree tu lui associes une isntance de ton renderer

myTree.setCellRenderer(new MyTreeCellRenderer()); // use a custom cell renderer
0