Results 1 to 8 of 8
- 03-15-2010, 02:10 PM #1
Member
- Join Date
- Mar 2010
- Posts
- 6
- Rep Power
- 0
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:
but it didn't work.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); } });
any ideas?
kind regards,
Yannay
- 03-15-2010, 02:28 PM #2
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
- 03-15-2010, 02:37 PM #3
Member
- Join Date
- Mar 2010
- Posts
- 6
- Rep Power
- 0
Thanks, but it didn't work.
- 03-15-2010, 02:38 PM #4
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
- 03-15-2010, 02:46 PM #5
Member
- Join Date
- Mar 2010
- Posts
- 6
- Rep Power
- 0
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)
- 03-15-2010, 03:04 PM #6
Member
- Join Date
- Mar 2010
- Posts
- 6
- Rep Power
- 0
as a matter of fact "getTreeCellRendererComponent" is never invoked.
- 03-15-2010, 03:59 PM #7
Member
- Join Date
- Mar 2010
- Posts
- 6
- Rep Power
- 0
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
- 03-15-2010, 04:05 PM #8
Glad it's working now :)
Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
The Ubiquitous Newbie Tips
Similar Threads
-
How to override DefaultTreeCellRenderer not to use "toString"
By yannay in forum SWT / JFaceReplies: 0Last Post: 03-15-2010, 12:29 PM -
how to override "cancel operation" in "progress bar"
By singswt in forum SWT / JFaceReplies: 2Last Post: 10-08-2009, 11:28 PM -
MoneyOut.println("It took you (whats wrong?>",year,"<WW?) years to repay the loan")
By soc86 in forum New To JavaReplies: 2Last Post: 01-24-2009, 06:56 PM -
Error! "filename" is not abstract and does not override abstract method...
By hasani6leap in forum New To JavaReplies: 6Last Post: 10-27-2008, 12:25 AM -
the dollar sign "$", prints like any other normal char in java like "a" or "*" ?
By lse123 in forum New To JavaReplies: 1Last Post: 10-20-2008, 07:35 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks