CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Sep 2000
    Location
    Singapore
    Posts
    5

    TreeCellRenderer Doubt

    Hi,
    Im not getting different icons for different
    files in a tree.
    setLeafIcon() changes all files to the same color

    My code is

    for(int i=0;i<list.length;i++)
    {
    if (list[i].equals("receiver") )
    {
    renderer.setLeafIcon(new ImageIcon("green.gif")
    );
    > > > setCellRenderer(renderer);
    > > > }
    > > > else if (list[i].equals("transmitter"))
    > > > {
    > > > renderer.setLeafIcon(new
    > > > ImageIcon("redfile.gif") );
    > > > setCellRenderer(renderer);
    > > > }
    > > > else if (list[i].equals("recupdated"))
    > > > {
    > > > renderer.setLeafIcon(new
    > > > ImageIcon("bluefile.gif") );
    > > > setCellRenderer(renderer);
    > > > }
    > > > else if (list[i].equals("transupdated"))
    > > > {
    > > > renderer.setLeafIcon(new
    > > > ImageIcon("yellowfile.gif") );
    > > > setCellRenderer(renderer);
    > > > }
    > > > }
    > > >
    For each condition it checks, it changes
    > all
    > > > icons to the same
    > > > color.So if the last file is "transupdated" all
    > the
    file icons change color
    to yellowfile.gif
    Try giving me a solution ..and if possible make changes in the particular code and send it

    Best Regards,
    Praveen.C

    When going gets tough,
    Tough gets going.

  2. #2
    Join Date
    Sep 2000
    Location
    Melbourne --> Australia
    Posts
    68

    Re: TreeCellRenderer Doubt

    Hi there
    I think you have got the TreeCellRenderer for the
    whole tree.
    What you need is one for each leaf,(using the
    getCellRenderer() method) then you
    can change the particular one ie:
    greenrenderer.setLeafIcon(new ImageIcon("green.gif")
    );
    > > > setCellRenderer(greenrenderer);
    > > > }
    else
    redrenderer.setLeafIcon(new ImageIcon("red.gif")
    );
    > > > setCellRenderer(redrenderer);
    > > > }

    I hope this helps
    Phill.



  3. #3
    Join Date
    Nov 1999
    Location
    Indianapolis, IN
    Posts
    191

    Re: TreeCellRenderer Doubt


    Here's an example, I didn't actually change the icons, but the hook is shown below.
    Hope this helps.


    /**
    * Over written to set the tool tips. The tool tip is simply the value, which is
    * the string which gets displayed. This is useful when the string is longer than the
    * on-screen component is wide.
    */
    public java.awt.Component getTreeCellRendererComponent(javax.swing.JTree tree, Object value, boolean sel,
    boolean expanded, boolean leaf, int row, boolean hasFocus)
    {
    super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
    if (true)
    {
    setToolTipText(value.toString());
    }
    else
    {
    setToolTipText(null); //no tool tip
    }

    //Optionally set the icons here
    //if (leaf)
    //{
    //this.setIcon(getDefaultLeafIcon()); //specify which icon heres
    //}
    return this;
    }









Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured