Solved

casting to custom TreeCellRenderer

Posted on 2010-11-12
6
669 Views
Last Modified: 2012-05-10
I'm creating my own TreeCellRender class and get a runtime error at some point in the program.  The error is

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: javax.swing.tree.DefaultTreeCellRenderer cannot be cast to some.package.name.MyTreeCellRenderer

I've included a couple lines of code and the entire custom TreeCellRenderClass in the code section.

It looks like it's complaining when I cast this.getCellRenderer() to MyTreeCellRenderer.  If I cast to DefaultTreeCellRenderer, I don't get that runtime error but then again, it never enters the MyTreeCellRendererClass.  I need to do that for the custom behavior (trying to change color of a single node).

Any ideas why it can't cast?

...
//DefaultTreeCellRenderer renderer = (DefaultTreeCellRenderer) this.getCellRenderer();
MyTreeCellRenderer renderer = (MyTreeCellRenderer) this.getCellRenderer();
...

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);
    MyTreeNode mytn = (MyTreeNode) value;

    if (mytn.getUploadStatus()) {
        setTextNonSelectionColor(Color.blue);
        setTextSelectionColor(Color.orange);
        setBackgroundSelectionColor(Color.white);
        setBorderSelectionColor(Color.black);
    }

    return this;
  }

}

Open in new window

0
Comment
Question by:mock5c
  • 3
  • 2
6 Comments
 
LVL 16

Expert Comment

by:Valeri
ID: 34124782
probably you have not set it?!
0
 

Author Comment

by:mock5c
ID: 34124949
What do you mean have not set it?  Do you mean setCellRenderer(renderer)?  The next time after
MyTreeCellRenderer renderer = (MyTreeCellRenderer) this.getCellRenderer();

is

this.setCellRenderer(renderer);
0
 
LVL 92

Accepted Solution

by:
objects earned 250 total points
ID: 34125166
> MyTreeCellRenderer renderer = (MyTreeCellRenderer) this.getCellRenderer();

remove that

> this.setCellRenderer(renderer);

and that should be:


this.setCellRenderer(new MyTreeCellRenderer());
0
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.

 

Author Comment

by:mock5c
ID: 34125228
OK, did that and that works.  If you don't mind me asking another quick question.  In my class above, I wanted it so that when a single node is selected, right clicked, marked for upload through popup menu (sets boolean uploadStatus in MyTreeNode to true), it should only change the color of that specific node.  Now that I don't get a runtime error with your fixes, it actually changes color for ALL nodes.  Shouldn't it be on the node I marked?
0
 
LVL 92

Expert Comment

by:objects
ID: 34125327
try adding an else in your renderer and setting the colours for when upload status is false
0
 

Author Comment

by:mock5c
ID: 34125536
Well, I seem be be getting some odd behavior.  Sometimes the root or branch nodes are changing color even though they are never marked.  I only intend to mark leaf nodes. And sometimes the wrong leaf node changes color.   I think I'll have to post this as a separate question since my problem with casting has been resolved.
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
topping3 challenge 14 70
Java Timer (static) 9 36
eclipse shortcuts 9 45
diffSum example 4 26
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

895 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

21 Experts available now in Live!

Get 1:1 Help Now