Results 1 to 8 of 8
  1. #1
    yannay is offline Member
    Join Date
    Mar 2010
    Posts
    6
    Rep Power
    0

    Default How to override DefaultTreeCellRenderer not to use "toString"

    Hi All,

    I am coding a small program which has a Swing GUI.
    In one of the panels, I have a JTree.
    I wrote my own Model for it and implemented the "getChild" methods as well as the other methods appropriately.

    Now, some of the leafs which the tree displays are "Classifier" (a class I wrote), and I don't want the tree to use the "Classifier.toString()" method when drawing the value but "ElementRenderer.toString(Classifier)" (where ElementRenderer is another class of mine).

    I tried to set a DeafaultTreeCellRenderer to the tree and override the "getTreeCellRendererComponent" method as following:

    Java Code:
    tree.setCellRenderer(new DefaultTreeCellRenderer(){
          public Component getTreeCellRendererComponent(
                                             JTree tree,
    					Object value,
                                            boolean sel,
                                            boolean expanded,
                                            boolean leaf,
    					int row,
                                            boolean hasFocus) {
               if (value instanceof Classifier)
    		value = ElementRenderer.toString((Classifier) value);
    	   return super.getTreeCellRendererComponent(tree,  value, sel, expanded, leaf,row, hasFocus);
    	}
    });
    but it didn't work.

    any ideas?

    kind regards,
    Yannay

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    Wrong order:
    Java Code:
    super.getTreeCellRendererComponent(tree,  value, sel, expanded, leaf,row, hasFocus);
    if (value instanceof Classifier)
    		value = ElementRenderer.toString((Classifier) value);
    return this;
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    yannay is offline Member
    Join Date
    Mar 2010
    Posts
    6
    Rep Power
    0

    Default

    Thanks, but it didn't work.

  4. #4
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    Ok, what do you expect? What do you get? What is ElementRenderer.toString((Classifier) value); supposed to return? Does it return that? Why don't you use setText()?
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  5. #5
    yannay is offline Member
    Join Date
    Mar 2010
    Posts
    6
    Rep Power
    0

    Default

    Now, when I am trying to make it work, I put a constant strings in Classifier.toString() and ElementRenderer.toString(Classifier)
    the former returns "Classifier.toString()". and the latter "ElementRenderer.toString(Classifier)". When it will work, I will change it.

    So I expect to see "ElementRenderer.toString(Classifier)", What I actually see is "Classifier.toString()". ElementRenderer.toString(Classifier) is never invoked.
    and setText doesn't work either. (using setText in this method)

  6. #6
    yannay is offline Member
    Join Date
    Mar 2010
    Posts
    6
    Rep Power
    0

    Default

    as a matter of fact "getTreeCellRendererComponent" is never invoked.

  7. #7
    yannay is offline Member
    Join Date
    Mar 2010
    Posts
    6
    Rep Power
    0

    Default

    nvm. its solved.

    there were two problems:
    1. The debugger (in Eclipse IDE) skipped the breakpoint in "getTreeCellRendererComponent" which made me think it's never invoked.
    2. The object in the Tree is not "Classifier" but "ClassifierNode" which is a class that encapsulated "Classifier". I wrote it couple of months ago and forgot about it. So this class' toString returns the encapsulated Classifier instance's toString which made me believe my implementation was wrong.

    Thank you very much for your help and sorry for bothering you.
    Yannay

  8. #8
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    Glad it's working now :)
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

Similar Threads

  1. Replies: 0
    Last Post: 03-15-2010, 12:29 PM
  2. Replies: 2
    Last Post: 10-08-2009, 11:28 PM
  3. Replies: 2
    Last Post: 01-24-2009, 06:56 PM
  4. Replies: 6
    Last Post: 10-27-2008, 12:25 AM
  5. Replies: 1
    Last Post: 10-20-2008, 07:35 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •