Solved

casting to custom TreeCellRenderer

Posted on 2010-11-12
6
663 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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
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 …

747 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

12 Experts available now in Live!

Get 1:1 Help Now