• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to mark checked CheckBox in JTree only when user click on CheckBox?

 
Premal Panchal
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

In my application I am showing JCheckBox with every node of JTree. My requirement is JCheckBox should be selected or deselected only when user click on JCheckBox not by clicking on the Node. Means if I click the text part of node it should not select or deselect the JCheckBox. I have seen various sites like CodeGuru etc. for getting JCheckBox with JTree, but they have not provided the solution of my problem. So is ther any way to achieve what I want? Any suggestion regarding this will be appreciated.

Thanx and Regards,

Premal Panchal
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure that is possible. I believe that when the JTree renders the JCheckBox it is not really rendering that actual component but a visual representation of that JCheckBox. The JTree handles check box selection by listening for which "node" you select and propigating an action down to the actual component that is being rendered.
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure it is. What you have to do is ignore tree selection events and listen for mouse clicks. Determine with each mouse click whether or not it was over a check box. I'll have to dig into the API a little bit if you want a specific example but it's certainly possible as I did just that in a project about a year ago.
 
Premal Panchal
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Gregg,

I have already tried, but when i got the point where mouse is clicked at that time i couldnt find check box at that.. i tried other ways also..if possible then try at ur convenience and try to give me sample code...

Thanx in advance,

Premal Panchal
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic