Solved

TreeCellRenderer icon not displaying during JTree cell edit.

Posted on 2001-06-17
2
810 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
wordmultiple challenge 12 116
Unable to start eclipse ? 17 128
how do i compare an object based on two fields 6 48
rhino JavaScript import, load 25 66
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
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.

932 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now