Solved

TreeCellRenderer icon not displaying during JTree cell edit.

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

757 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

20 Experts available now in Live!

Get 1:1 Help Now