?
Solved

casting to custom TreeCellRenderer

Posted on 2010-11-12
6
Medium Priority
?
692 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
[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
  • 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 1000 total points
ID: 34125166
> MyTreeCellRenderer renderer = (MyTreeCellRenderer) this.getCellRenderer();

remove that

> this.setCellRenderer(renderer);

and that should be:


this.setCellRenderer(new MyTreeCellRenderer());
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
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 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 …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month10 days, 6 hours left to enroll

762 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