Solved

casting to custom TreeCellRenderer

Posted on 2010-11-12
6
673 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
recursion example 16 124
servlet doXXX methods 3 48
how to add new optional parameter to JSP 1 40
javap not working 8 44
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

786 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