Solved

casting to custom TreeCellRenderer

Posted on 2010-11-12
6
678 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

749 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