• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

hard! how to use a compound node (JTextAreas + JButtons) in a JTree?

 
Ranch Foreman
Posts: 531
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
I have 2 JTextAreas and some JButtons in a Box layout - this is one node. I wish to display these nodes in a JTree. The Text Areas are editable.
Does anyone know how I can do this? I could not find any examples of this (for compound Swing components) on the web.
thanks,
Anil


[ January 28, 2006: Message edited by: Anil Philip ]
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's the tutorial on JTree:

http://java.sun.com/docs/books/tutorial/uiswing/components/tree.html

It includes some examples of TreeCellRenderers, which are what you are asking about. Most of the examples you'll find have the renderer extending JLabel, because commonly a JLabel is sufficient to render a tree node. But in your case the renderer will have to be some other component, maybe JPanel or a customized subclass.
 
Anil Philip
Ranch Foreman
Posts: 531
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am using class VisualNode below as my user object
Ok, so I am using my own renderer because this is a compound node with JButtons, JTabbedPane, JTextArea in a Box.
When I create the node, I do a:

DefaultMutableTreeNode childNode = new DefaultMutableTreeNode(new VisualNode("My Name"));
parentNode.add(childNode);

I have 3 questions:
1) DefaultTreeCellRenderer extends JLabel, but VisualNode contains several JComponents. So is it correct for MyCellRenderer to extend DefaultTreeCellRenderer?
2) How do I access the user object? In the renderer, I want to be able to get this user object so that I can return the Box component.
3) What is the parameter "Object value" for a Compound node? If I add the Editor, how will it know which it stands for? (the title or the details)




Originally posted by Paul Clapham:
Here's the tutorial on JTree:

http://java.sun.com/docs/books/tutorial/uiswing/components/tree.html

It includes some examples of TreeCellRenderers, which are what you are asking about. Most of the examples you'll find have the renderer extending JLabel, because commonly a JLabel is sufficient to render a tree node. But in your case the renderer will have to be some other component, maybe JPanel or a customized subclass.

 
Anil Philip
Ranch Foreman
Posts: 531
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried:

and got:
 
Anil Philip
Ranch Foreman
Posts: 531
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am able to retrieve the user object now and am working on the other questions.

I am surprised that it is not stated in the JDK documentation that value is a DefaultMutableTreeNode:

getTreeCellRendererComponent Configures the renderer based on the passed in components. The value is set from messaging the tree with convertValueToText, which ultimately invokes toString on value. The foreground color is set based on the selection and the icon is set based on on leaf and expanded.

 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Anil Philip:
I have 3 questions:
1) DefaultTreeCellRenderer extends JLabel, but VisualNode contains several JComponents. So is it correct for MyCellRenderer to extend DefaultTreeCellRenderer?
2) How do I access the user object? In the renderer, I want to be able to get this user object so that I can return the Box component.
3) What is the parameter "Object value" for a Compound node? If I add the Editor, how will it know which it stands for? (the title or the details)

1. If you don't want your rendering component to BE a JLabel then clearly you shouldn't extend something that IS a JLabel. Just implement the TreeCellRenderer interface instead. The API documentation for TreeCellRenderer claims the tutorial has an example of using it.

2. The "user object" of the tree node is passed to your getTreeCellRendererComponent method as the second parameter (named "value" in your example).

3. That parameter is the user object of the tree node. It's up to you to write code that renders it in whatever way you want it rendered. If you want it to be editable (your original description didn't look like that) then you also need to write a TreeCellEditor.
 
Anil Philip
Ranch Foreman
Posts: 531
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, but your advice was incorrect - it was not necessary to implement the TreeCellRenderer interface. Returning the Box, rendered it automatically since the Box is a JComponent and contains JComponents. The code in my thread above displays the compound node just fine. Simply extending DefaultTreeCellRenderer worked!
thanks,
Anil

Originally posted by Paul Clapham:
1. If you don't want your rendering component to BE a JLabel then clearly you shouldn't extend something that IS a JLabel. Just implement the TreeCellRenderer interface instead. The API documentation for TreeCellRenderer claims the tutorial has an example of using it.



[ February 11, 2006: Message edited by: Anil Philip ]
[ February 12, 2006: Message edited by: Anil Philip ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic