File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes JTree with CheckBox Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JTree with CheckBox" Watch "JTree with CheckBox" New topic

JTree with CheckBox

Lalita Kadambala

Joined: Jun 08, 2007
Posts: 2
I want to make a JTree with checkboxes. The checkbox selection should be independent of the node selection. i.e. if I click on the node's text part it should be highlighted but the check box should not be ticked/unticked. Ticking/unticking the checkbox should not highlight the node's text part.

Actually when the GUI comes up for the first time i read the file system of a system and displays only the first level, say only the C: drive and its folders in the left pane as tree and on right side pane the files present in C drive in a tabular structure.

Now when i click on the node's text part, node should be highlited and it should fetch the content of the folder and add the subfolders to it and so on. This means I am adding the child nodes on user click.
May be I can have a FolderUserObject which can save the tick/untick as a boolean variable, as i need this at later part.

Please provide me some good sample code soon.
Its very urgent.
Thanks in advance.
Bridget Kennedy
Ranch Hand

Joined: Nov 30, 2004
Posts: 86
I recently researched this topic. Googling 'jtree checkbox' returned many results. I found this example to be very useful.
[ June 08, 2007: Message edited by: Bridget Kennedy ]
Lalita Kadambala

Joined: Jun 08, 2007
Posts: 2
Problem with this example code is it checks box is not there for the parent nodes. Can somebody correct this so that i should have checkboxs through out the tree but the behaviour of the checkbox should ramain same.

Is it possible to remove the if(leaf) check in the renderer?

Please provide me the solution.
Thanks in advance.
I agree. Here's the link:
subject: JTree with CheckBox
It's not a secret anymore!