Solved

TreeCellRenderer icon not displaying during JTree cell edit.

Posted on 2001-06-17
2
817 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
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 …
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

707 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