• Post Reply Bookmark Topic Watch Topic
  • New Topic

JCheckBoxes in JTree nodes

 
Tris Rabar
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I'm fairly new with JTrees and in fact, Java on the whole. I've created a JTree with JCheckBoxes as the nodes. How do you implement listeners on such a component? For eg, can I use an ItemEvent listener for the checkboxes, while using a TreeSelectionListener for the nodes? I want to be able to get the data at the node that was checked, which my tre does right now, but also when the box is deselected, I want to perform some action. Can this be done with an ItemListener on the checkbox??
Please, any assistance is appreciated. It's urgent!
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't think of a reason why you couldn't add an ItemListener to a JCheckBox no matter what its container was. So go for it. By the way if for some reason it doesn't work let us all know.
 
shawn larkin
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I hope I'm wrong, but I think if you set the JComboBox as the Renderer for JTree nodes that's all it is, it tells it how to render. JTree has a different model for Rendering and Selection. But I'm probably wrong about this
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I stand corrected (ran a little test that flopped). I have not had much practical experience with JTrees other than using them as a navigation aid with Strings at the nodes. Unfortunately JTree is not as robust as JTable but I am sure you can accomplish your task if you stay with it. I just fear it may get pretty involved.
I will definitely investigate this further because we're all here to learn.
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Add a mouse listener to your tree node... check and see if the getPoint() of the click falls within the bounds of the check box on the tree node. If so, toggle the check box.

Easy!

-Nate
 
Tris Rabar
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey...thanks guys!
I will try the mouseListener and let you know if it works. Do any of you know about a getOldLeadSelectionPath() method?? I read this in another post...it was an option for determining the deselected node.
I will keep you posted.
Thanks again!!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!