🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.swing
Article #10978

Re: Icons for each node in a Tree

#10978
From: Jonathan
Date: Fri, 23 Jul 1999 00:00
207 lines
5560 bytes
this is the code i wrote, firstly you should be extending Jlabel and
implementing the tree Renderer interface,


this will work, if you need any help just ask:

public class treeRenderer extends JLabel implements TreeCellRenderer
{
        static protected ImageIcon        Icon1;
    static protected final Color SelectedBackgroundColor =
SystemColor.textHighlight;//new Color(0, 0, 128);



    /** Whether or not the item that was last configured is selected. */
    protected boolean            selected;


 public treeRenderer(){
 try {
     defaultFont = new Font("SansSerif", 0, 12);
 } catch (Exception e) {}
 try {
     Icon1 = new ImageIcon(<name of icon>.gif);

// you can have as many icons as you want

 } catch (Exception e) {
     System.out.println("Couldn't load images: " + e);
 }
    }




      public Component getTreeCellRendererComponent(JTree tree, Object
value,
       boolean selected, boolean expanded,
       boolean leaf, int row,
        boolean hasFocus) {
 Font            font;
 String          stringValue = tree.convertValueToText(value, selected,
        expanded, leaf, row, hasFocus);
  String val =  value.toString();

 /* Set the text. */
 setText(stringValue);
 /* Tooltips used by the tree. */
 setToolTipText(stringValue);
  String t =stringToken(val);
       if(<some thing>)
          setIcon(Icon1);
        else if(<something>)
         ..
        ..

 /* Update the selected flag for the next paint. */
 this.selected = selected;

 return this;
    }
    public void paint(Graphics g) {
 Color            bColor;
 Icon             currentI = getIcon();

 if(selected)
     bColor = SelectedBackgroundColor;
 else if(getParent() != null)

     bColor = getParent().getBackground();
 else
     bColor = getBackground();
 g.setColor(bColor);
 if(currentI != null && getText() != null) {
     int          offset = (currentI.getIconWidth() + getIconTextGap());

     g.fillRect(offset, 0, getWidth() - 1 - offset,
         getHeight() - 1);
 }
 else
     g.fillRect(0, 0, getWidth()-1, getHeight()-1);
 super.paint(g);
    }
}

Firoz T M wrote in message <379871C9.588B936C@procsys.com>...
>Hi,
>
>   am porting an existing C++ app to Java. This application contains a
>TREE CONTROL. Each main node is having a different ICONS....
>
>   How can I achieve this???? If any one know, please let me know...
>
>   Inorder to achieve this, I wrote a class which uses JTree in it..
>This class uses My own derived Renderer.... But when using this, itz not
>displaying any icons....
>
>   Any help is appreciated.....
>
>
>The Render class is as follows...
>----------------------------------
>
>
>    class WFCTreeRenderer extends DefaultTreeCellRenderer {
>        public WFCTreeRenderer() {
>        }
>
>        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);
>
>          Icon icon = ((IconNode)value).getIcon();
>
>          if (icon == null)
>          {
>            String name = ((IconNode)value).getIconName();
>            if (name != null)
>            {
>              icon = (Icon)icons.get(name);
>              if (icon != null)
>              {
>                setIcon(icon);
>              }
>            }
>          }
>          else
>          {
>            setIcon(icon);
>          }
>
>          return this;
>        }
>
>
>    }
>
>IconNode.java
>-------------
>
>import java.util.*;
>import javax.swing.*;
>import javax.swing.tree.*;
>
>
>public class IconNode extends DefaultMutableTreeNode  {
>
>  protected Icon   icon;
>  protected String iconName;
>
>  public IconNode() {
>    this(null);
>  }
>
>  public IconNode(Object userObject) {
>    this(userObject, true, null);
>  }
>
>  public IconNode(Object userObject, boolean allowsChildren
>        , Icon icon) {
>    super(userObject, allowsChildren);
>    this.icon = icon;
>  }
>
>
>
>  public void setIcon(Icon icon) {
>    this.icon = icon;
>  }
>
>  public Icon getIcon() {
>    return icon;
>  }
>
>  public String getIconName()
>  {
>      return iconName;
>  }
>
>  public void setIconName(String name) {
>    iconName = name;
>  }
>
>}
>
>
>
>
>
>--
>----------------------------------------------------------------
>FIROZ T M                          firoz@procsys.com
>----------------------------------------------------------------
>
>Quote for the day
>Please Tell Me if you Don't Get This Message
>----------------------------------------------------------------
>This is through WebEtc Mail Server.....
Technical Details
Message-ID: <7na11v$oqt26@forums.borland.com>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <379871C9.588B936C@procsys.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 111d45,240329fa40c08b00
X-Google-Attributes: gid111d45,public
From: "Jonathan" <plaskowj@logica.com>
Subject: Re: Icons for each node in a Tree
Date: 1999/07/23
Message-ID: <7na11v$oqt26@forums.borland.com>
X-Deja-AN: 504428994
References: <379871C9.588B936C@procsys.com>
Organization: Another Netscape Collabra Server User
X-MimeOLE: Produced By Microsoft MimeOLE V4.72.3110.3
Newsgroups: borland.public.jbuilder.java.swing