aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Can I add radiobuttons as leaves in a JTree? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Can I add radiobuttons as leaves in a JTree?" Watch "Can I add radiobuttons as leaves in a JTree?" New topic
Author

Can I add radiobuttons as leaves in a JTree?

Pomchi Bedi
Ranch Hand

Joined: Apr 03, 2001
Posts: 55
Hi All,
I want the rest of the tree,it's behaviour everything to be the same except the leaves.I want to add radio buttons in place of the leaves.How can I do that??
Thanks
Pomchi
Matt Senecal
Ranch Hand

Joined: Dec 01, 2000
Posts: 255
Yes, it is possible to do. In it's standard form a JTree cannot put Swing components into nodes because it does not "know" how to render them. You can change that by creating and implementing a custom TreeCellEditor and TreeCellRenderer for your JTree. Create the Editor/Renderer so that it allows a JPanel (or whatever container you want to use) to be put in a node. Then you can add whatever you want to the JPanel for that node, and it will be displayed.
Take a look at this previous post of mine for an example:
http://www.javaranch.com/ubb/Forum2/HTML/001095.html
And please let me know if you have any further questions.

Originally posted by Pomchi Bedi:
Hi All,
I want the rest of the tree,it's behaviour everything to be the same except the leaves.I want to add radio buttons in place of the leaves.How can I do that??
Thanks
Pomchi


[This message has been edited by Matt Senecal (edited September 25, 2001).]


There's nothing more dangerous than a resourceful idiot. ---Dilbert
Pomchi Bedi
Ranch Hand

Joined: Apr 03, 2001
Posts: 55
Hi Matt,
It's you again to help me out.Last time also it was because of you that I was able to solve my problem.
Alright,here we go.......
I know that if we have to put Swing components in place of nodes we can do that by creating and implementing a custom TreeCellEditor and TreeCellRenderer for the JTree.It's easy if we have to put the same component for every node.But this time I want the rest of the tree to remain as it is.I just want my own components in place of the leaves(I want a group of radio buttons as leaves).Can I do the setCellEditor & setCellRenderer for just the leaves...not for the entire tree?I guess 'NO'.Please help me out with some solution.
Thanks
Pomchi
Matt Hansen
Ranch Hand

Joined: Jul 31, 2001
Posts: 34
When you implement getTreeCellRendererComponent it includes a leaf parameter which will tell you if this component is a leaf or not. You can use this information to decide how to render it.
Pomchi Bedi
Ranch Hand

Joined: Apr 03, 2001
Posts: 55
Hi Matt,
I tried that and by this check I can put radio buttons as leaves.Can you please tell me if the parent nodes are normal nodes(DefaultMutableTreeNode) then what should I return if leaf=false in getCellRendererComponent(.....).
If leaf=true then I'll create my own component and will return that,but outside this if statement also I need to return something.I tried a lot of things JComponent,Component etc.....but nothing seems to work.
As a workaround what I've done is-- for parent nodes I am creating and returning JLabel.It works but I have to insert icons on my own,I have to worry about highlighting the selected node,.....lots of stuff.
If you have a better way to do it please let me know.
Thanks
Pomchi
Matt Senecal
Ranch Hand

Joined: Dec 01, 2000
Posts: 255
I can't think of a better method right off the bat.
Originally posted by Pomchi Bedi:
Hi Matt,
I tried that and by this check I can put radio buttons as leaves.Can you please tell me if the parent nodes are normal nodes(DefaultMutableTreeNode) then what should I return if leaf=false in getCellRendererComponent(.....).
If leaf=true then I'll create my own component and will return that,but outside this if statement also I need to return something.I tried a lot of things JComponent,Component etc.....but nothing seems to work.
As a workaround what I've done is-- for parent nodes I am creating and returning JLabel.It works but I have to insert icons on my own,I have to worry about highlighting the selected node,.....lots of stuff.
If you have a better way to do it please let me know.
Thanks
Pomchi

Matt Hansen
Ranch Hand

Joined: Jul 31, 2001
Posts: 34
What I did to make leaf nodes checkboxes is extend JCheckBox and implemented TreeCellRenderer for my cell renderer. Actually, I think that the leaves are the only thing that end up being drawn as checkboxes.
Pomchi Bedi
Ranch Hand

Joined: Apr 03, 2001
Posts: 55
Thanks to both of you for your reply.
I am still rendering JLabel as parent nodes and JRadioButton as leaves.I have put the folder.gif icon along with the name of the label.So my tree is looking good.....just like the original tree.
Just one problem is there that I am not been able to fix, i.e. to highlight the parent nodes when the user clicks on the node.In my function getCellEditorComponent I set the background of the label to blue.The background changes on selection of that node but the background of the text(of the label) as well the icon is getting changed.
Although in windows explorer just the text(name of the folder/file) gets highlighted.It looks really neat as compared to my tree (with both icon + text getting highlighted) ;-( Can you help me with this??.How can I set the background of just the text of my Label.
Thanks
-Pomchi
 
 
subject: Can I add radiobuttons as leaves in a JTree?
 
Similar Threads
Can we and navigation rule dynamically?
HashMap values
How to read and wrire date in RMS
JTree
SOAP header in SOAP request