class ComponentTreeCellRenderer extends DefaultTreeCellRenderer {
public Component getTreeCellRendererComponent(JTree
aTree,Object
aValue,boolean aSelected,boolean aExpanded,boolean aLeaf,int
aRow,boolean
aHasFocus) {
}
in call getTreeCellRendererComponent the aValue object that is passed is instance of treenode... normally DefaultMutableTreeNode
.. hence the case fails..
what you want to do
DefaultMutableTreeNode node = (DefaultMutableTreeNode)aValue
JButton button = node.getUserObject();
hope this helps.
-Premal
>
>
> Question: Why is it that DefaultMutableTreeNode contains
> an Object, and
> the value parm
> DefaultTreeCellRenderer.getTreeCellRendererComponent is an
> Object,
> but somewhere, somehow, any object you send gets forcably
> changed into a String?
> For instance, if I send this to the attached code:
> addItem(new JButton("Hello World"),null);
> The (Component) cast fails, and the tree gets a JLabel
> that says stuff
> like:
>
> "....flag=1134,maximumSize=,minimumSize=,preferredSize=,deaful
> tIcon=,disabledIcon=,disabledSelectedIcon=,
>
> ..." (you get the picture)
> I have considered just keeping an ArrayList of
> everything, and then just
> sending an index across the JTree mechanism.
> Since this is a silly and stupid thing to have to do, I was
> wondering if anybody
> knows how to disable the ridiculous(IMHO)
> toString that's happening somewhere.
>
> P.S. Here's the sample code...
>
> class ComponentTreeCellRenderer extends
> DefaultTreeCellRenderer {
> public Component getTreeCellRendererComponent(JTree
> aTree,Object
> aValue,boolean aSelected,boolean aExpanded,boolean aLeaf,int
> aRow,boolean
> aHasFocus) {
> Component retval;
> try {
> retval=(Component)aValue;
> } catch(ClassCastException exception) {
> retval=new JLabel(aValue);
> }
> return retval;
> }
> };
>
> DefaultMutableTreeNode addItem(Object
> aItem,DefaultMutableTreeNode aNode) {
> DefaultTreeModel model=(DefaultTreeModel)tree.getModel();
> DefaultMutableTreeNode newNode=new
> DefaultMutableTreeNode(aItem);
> DefaultMutableTreeNode current=aNode;
> if(current==null) {
> current=(DefaultMutableTreeNode)model.getRoot();
> }
> model.insertNodeInto(newNode,current,0);
> return newNode;
> }
>
>
> _______________________________________________
> Advanced-swing mailing list
> [EMAIL PROTECTED]
> http://eos.dk/mailman/listinfo/advanced-swing
>
