Solved

TreeCellRenderer icon not displaying during JTree cell edit.

Posted on 2001-06-17
2
811 Views
Last Modified: 2008-03-04
I have customized the icon displayed for a specific type of JTree node by subclassing DefaultTreeCellRenderer, which work fine:

public class MyTreeCellRenderer extends DefaultTreeCellRenderer {

  public MyTreeCellRenderer() {
  }

         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);
            ImageIcon ii = new ImageIcon(getClass().getResource("/myicon.gif"));
               if ( condition...) {
                   this.setIcon(ii);
               }
            return this;
        }
}

My problem is that when an edit is invoked on a node that has the custom icon set, the default (folder) icon is displayed during the edit.

I have tried subclassing DefaultTreeCellEditor, but that doesn't seem to work.  I am calling the JTree setCellEditor and setCellRenderer as follows:

this works =>

    MyTreeCellRenderer tcr = new MyTreeCellRenderer();
    jTree.setCellRenderer(tcr);

but this doesn't=>
    MyTreeCellEditor tce = new MyTreeCellEditor(jTree,jTree.getCellRenderer());
    jTree.setCellEditor(tce);


Here is MyTreeCellEditor...


public class MyTreeCellEditor extends DefaultTreeCellEditor {
  public MyTreeCellEditor(JTree tree, TreeCellRenderer renderer){
    super(tree,(DefaultTreeCellRenderer)renderer);
  }

  public java.awt.Component getTreeCellEditorComponent(JTree tree,Object value,
             boolean isSelected, boolean expanded,
              boolean leaf, int row) {

      java.awt.Component comp = super.getTreeCellEditorComponent(tree,value,isSelected,expanded,leaf,row);
      return(comp);
  }
}


Thanks for your help,

Dave Connerth (captaindave2)

0
Comment
Question by:captaindave2
2 Comments
 
LVL 9

Accepted Solution

by:
Ovi earned 300 total points
ID: 6201130
Usually for avoiding such problems, the cellEditor and cellRenderer must point to the same Object.
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 8950795


No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:


--  points to Ovi


Please leave any comments here within the next seven days.
 
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
 
sudhakar_koundinya
EE Cleanup Volunteer
---------------------
If you feel that your question was not properly addressed, or that none of the comments received were appropriate answers, please post your concern in THIS thread.
0

Featured Post

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question