Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Storing a userObject in JTree node and displaying text...

843805Feb 22 2007 — edited Oct 13 2008
Hello,

I have a simple beginner's problem, and I'd greatly appreciate some advice. I have a JTree in which the leaf nodes have been assigned a class using .setUserObject(). This allows me to fetch the object using .getUserObject when my TreeListener is triggered and perform some actions on the class after casting. My problem is that adding the userObject eliminates my control over the displayed text. I've created a extended version of DefaultCellRenderer to deal with this problem (using the JTree tutorial as a template), but I need to be able to fetch information from the UserObject to format the appropriate text for the leaf nodes. Any advice on how to accomplish this would be much appreciated.

Thanks!
P.S. below is the skeleton of the extended renderer... basically copied from the JTree tutorial.

   class MyRenderer extends DefaultTreeCellRenderer {
  
        public MyRenderer() {
         }

        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);
            if (leaf) {
            	this.setText("This part works... but how to fetch the leaf-specific info to add the correct text???");        	
            } 
            return this;
        }

    }
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 10 2008
Added on Feb 22 2007
4 comments
394 views