TreeCellRenderer icon not displaying during JTree cell edit.

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) {

                            tree, value, sel,
                            expanded, leaf, row,
            ImageIcon ii = new ImageIcon(getClass().getResource("/myicon.gif"));
               if ( condition...) {
            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();

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

Here is MyTreeCellEditor...

public class MyTreeCellEditor extends DefaultTreeCellEditor {
  public MyTreeCellEditor(JTree tree, TreeCellRenderer 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);

Thanks for your help,

Dave Connerth (captaindave2)

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Usually for avoiding such problems, the cellEditor and cellRenderer must point to the same Object.

Experts Exchange Solution brought to you by ConnectWise

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial

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.
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.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.