Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6221305
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T08:06:34+00:00 2026-05-24T08:06:34+00:00

I need to create a component as shown in the figure – a tree

  • 0

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.)?

enter image description here

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T08:06:34+00:00Added an answer on May 24, 2026 at 8:06 am

    Is there is a built in component for this (like others such as JFileChooser etc.)?

    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

    ..a tree with directory selection via checkbox.

    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.

    Check-box tree

    import java.awt.*;
    import javax.swing.*;
    import javax.swing.tree.*;
    import java.io.*;
    
    class TreeWithCheck {
    
        public static Component getTree(int selectionMode) {
            JTree tree = new JTree();
            tree.getSelectionModel().setSelectionMode(selectionMode);
            tree.setVisibleRowCount(8);
            SelectableTreeCellRenderer renderer =
                new SelectableTreeCellRenderer();
            tree.setCellRenderer(renderer);
            JScrollPane scroll = new JScrollPane(tree);
            Dimension d = scroll.getPreferredSize();
            scroll.setPreferredSize(
                new Dimension((int)d.getWidth()*2, (int)d.getHeight()));
    
            return scroll;
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    JPanel trees = new JPanel(new GridLayout(0,2,5,5));
    
                    trees.add(
                        getTree(TreeSelectionModel.SINGLE_TREE_SELECTION));
                    trees.add(
                        getTree(TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION));
    
                    JOptionPane.showMessageDialog(null, trees);
                }
            });
        }
    }
    
    class SelectableTreeCellRenderer extends DefaultTreeCellRenderer {
    
        private JCheckBox selected;
        private JPanel renderComponent;
    
        public SelectableTreeCellRenderer() {
            selected = new JCheckBox();
            renderComponent = new JPanel(new BorderLayout());
            renderComponent.add(selected,BorderLayout.WEST);
    
            selected.setOpaque(false);
            renderComponent.setOpaque(false);
        }
    
        public Component getTreeCellRendererComponent(
            JTree tree,
            Object value,
            boolean sel,
            boolean expanded,
            boolean leaf,
            int row,
            boolean hasFocus) {
    
            Component c = super.getTreeCellRendererComponent(
                tree,
                value,
                false, // we pass 'false' rather than 'sel'
                expanded,
                leaf,
                row,
                hasFocus);
    
            selected.setSelected(sel);
            renderComponent.add(c,BorderLayout.CENTER);
    
            return renderComponent;
        }
    }
    

    Define a custom object

    One way you might approach it is to put custom objects into the tree, rather than File instances. The custom object might be called a SelectableDirectory and have a File directory & boolean selected as the attributes. Something like:

    public class SelectableDirectory {
    
        private File directory;
        private boolean selected;
        // getters and setters, constructor..
    }
    

    Use a custom renderer for the tree that returns a JPanel containing a checkbox in BorderLayout.WEST and a JLabel with icon and directory name in the CENTER.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to create a custom GUI Component about same like shown in the
I need to create an XML schema that looks something like this: <xs:element name=wrapperElement>
I need help with this question. I'm using the camel-http component as shown here
I need to create the UI component shown in the image below, and also
I need to create a custom Component with JSF 2.0 (not composite component), that
I have a component that create dynamically html table. According to my need, I
I've got a custom component that I need to use if I create a
I need to create a custom component in Flex 4.5 by extending the spark
I would like to create a component(extending from spark Panel), which upon a buttonclick
I have found some similar questions like this one, however there are so many

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.