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 8192215
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T04:07:38+00:00 2026-06-07T04:07:38+00:00

I am creating a JTree using the GUI builder in Netbeans and I can

  • 0

I am creating a JTree using the GUI builder in Netbeans and I can add nodes and everything to the tree using the following code

public static void listAllFiles(String directory, DefaultMutableTreeNode parent, Boolean recursive) {
            File [] children = new File(directory).listFiles(); // list all the files in the directory
            for (int i = 0; i < children.length; i++) { // loop through each
                    DefaultMutableTreeNode node = new DefaultMutableTreeNode(children[i].getName());
                    // only display the node if it isn't a folder, and if this is a recursive call
                    if (children[i].isDirectory() && recursive) {
                            parent.add(node); // add as a child node
                            listAllFiles(children[i].getPath(), node, recursive); // call again for the subdirectory
                    } else if (!children[i].isDirectory()){ // otherwise, if it isn't a directory
                            parent.add(node); // add it as a node and do nothing else
                    }
            }
    }

then calling it like

listAllFiles("C:\\test",  defaultMutableTreeNode , true);

I can add this code to the init() method of the JTree so that when it is built it will have all the folders and files in the Test folder which is grand out, but what i really want to do is add the nodes to the JTree when i click on a button but i cant figure out how to do this! I can add the listAllFiles("C:\\test", defaultMutableTreeNode , true); to the ActionPerformed of the a new button but then it cannot find the defaultMutableTreeNode.

So how would be the best way to do this? would it be to create a new DefaultMutableTreeNode when ever I click the button?

  • 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-06-07T04:07:40+00:00Added an answer on June 7, 2026 at 4:07 am

    Well i figured out one way to do it but im not too sure if is the best way to do it! I basically create a new DefaultMutableTreeNode in the ActionPerformed of the button and that is populating the tree correctly for me anyway

    javax.swing.tree.DefaultMutableTreeNode treeNode1 = new javax.swing.tree.DefaultMutableTreeNode("root");
    jTree.setModel(new javax.swing.tree.DefaultTreeModel(treeNode1));
    listAllFiles(folderPath,  treeNode1, true);
    

    but would like to see if there is any better ways to do this… coding wise

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

Sidebar

Related Questions

Creating a table without tbody using javascript createElement/appendChild will not add tbody in Firebug
Creating a searchable contacts kind of app. Below is my code public class EmployeeList
Creating MVC3 Razor Helper like Helper.BeginForm() says that it can be done using extension
I am having difficulty creating a JTree that allows the nodes to be reorganized
Creating a view with following code. - (void)loadView { paintView=[[UIView alloc]initWithFrame:CGRectMake(0, 50, 320, 430)];
I am creating a node by using crrm as below $(#TreeDiv).jstree(create, $(#somenode), inside, {
Creating a simple RPG game, first time using XNA. Trying to get my character
I am using jsTree for creating a documentation list index. I use JSON to
Creating sprite sheets aka texture atlases rather than using many hundres of individual images
Creating a site in with FrogCMS and using alot of jquery to create an

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.