I need to create a component as shown in the figure – a tree with directory selection via checkbox. Is there is a built in component for this (like others such as JFileChooser etc.)?

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
No, not a ready-made component for that specific purpose. OTOH it is not very difficult to throw something together using a
JTree.You might start with the code shown on https://stackoverflow.com/questions/6182110/file-browser-gui. It uses a tree.
Nodes with check-boxes
The selection via. checkbox makes it a bit more tricky. Here are two possible approaches.
Use a renderer
This allows multi-selection in the same way that multiple nodes might be selected in a standard tree, but swaps the BG selection color for a checked check-box.
It then becomes very easy to use the inbuilt methods such as
JTree.getSelectionPaths()and handles multi/single selection with a single method call.Define a custom object
One way you might approach it is to put custom objects into the tree, rather than
Fileinstances. The custom object might be called aSelectableDirectoryand have aFile directory&boolean selectedas the attributes. Something like:Use a custom renderer for the tree that returns a
JPanelcontaining a checkbox inBorderLayout.WESTand aJLabelwith icon and directory name in theCENTER.