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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T16:48:48+00:00 2026-05-12T16:48:48+00:00

I have a JTree in which the user can drag/drop or re-arrange nodes, upon

  • 0

I have a JTree in which the user can drag/drop or re-arrange nodes,
upon saving I have to re-arrange the nodes such that File type nodes must appear
before Folder type nodes. I don’t need to sort the files/folders name.

User Tree:

 -FolderA
   +FFA1
   -FA1
   -FA2
 -FolderB
   -FB1
 -File1
 -File2
 +FolderC
 -File3

Resulting Tree:

-File1
 -File2
 -File3
 -FolderA   
   -FA1
   -FA2
   +FAF1
 -FolderB
   -FB1
 +FolderC

I have the following codes below, it worked but I don’t know if it is the proper way or the good practice perhaps.
Can you suggest which of the 2 solutions is better, or can you suggest other way.

Thanks you very much.

Solution 1:

private void arrange(DefaultMutableTreeNode parent){
    DefaultMutableTreeNode sorted = new DefaultMutableTreeNode();
    List<DefaultMutableTreeNode> files = new ArrayList<DefaultMutableTreeNode>();
    List<DefaultMutableTreeNode> folders = new ArrayList<DefaultMutableTreeNode>();

    for (int i = 0; i < parent.getChildCount(); i++){
        DefaultMutableTreeNode node = (DefaultMutableTreeNode) parent.getChildAt(i);
        int type = ((BusinessObject) node.getUserObject()).getType();
        if (type == BusinessObject.FILE)
            files.add(node);
        else{
            arrange(node);
            folders.add(node);
        }
    }
    for (int i = 0; i < files.size(); i++)
        sorted.add((DefaultMutableTreeNode) files.get(i));

    for (int i = 0; i < folders.size(); i++)
        sorted.add((DefaultMutableTreeNode) folders.get(i));

    while (sorted.getChildCount() > 0)
        parent.add((DefaultMutableTreeNode) sorted.getChildAt(0));

    sorted = null;
    files = null;
    folders = null;
}

Solution 2:

private void arrange(DefaultMutableTreeNode parent){
    DefaultMutableTreeNode sorted = new DefaultMutableTreeNode();
    List<DefaultMutableTreeNode> nodes = new ArrayList<DefaultMutableTreeNode>();

    for (int i = 0; i < parent.getChildCount(); i++){
        DefaultMutableTreeNode node = (DefaultMutableTreeNode) parent.getChildAt(i);
        int type = ((BusinessObject) node.getUserObject()).getType();
        if (type == BusinessObject.FILE)
            nodes.add(node);
    }

    for (int i = 0; i < parent.getChildCount(); i++){
        DefaultMutableTreeNode node = (DefaultMutableTreeNode) parent.getChildAt(i);
        int type = ((BusinessObject) node.getUserObject()).getType();
        if (type == BusinessObject.FOLDER){
            arrange(node);
            nodes.add(node);
        }
    }

    for (int i = 0; i < nodes.size(); i++)
        sorted.add((DefaultMutableTreeNode) nodes.get(i));

    while (sorted.getChildCount() > 0)
        parent.add((DefaultMutableTreeNode) sorted.getChildAt(0));

    sorted = null;
    nodes = null;
}
  • 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-12T16:48:48+00:00Added an answer on May 12, 2026 at 4:48 pm

    I think both are fine solutions. It was pretty easy to tell what they were doing: pull out the files, pull out the folders, throw them back in the tree in the right order. Also, the recursive call was straight-forward and intuitive.

    Pick whichever seems the most natural to you. The second seems more like the way I would do it, but that’s just me, and there’s not much difference.

    Are you using Java 5 or 6? If so, use for-each loops. Also, you don’t have to clear that values of your private variables at the end of the method. They go away anyhow when the method returns.

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

Sidebar

Related Questions

i have a JTree in Java with different parent nodes such as computers, gaming
I have a JTree which I give objects that implement the TreeNode interface, and
I have a JTree which displays a JPopupMenu when I right click nodes in
I'm writing a Java Tree in which tree nodes could have children that take
I have a JTree that I use as a file tree. If I choose
I have a JTree object which uses DefaultTreeModel as model and I add/remove nodes
I have created a JTree which shows a list of files. these files have
I have a created a following renderer which renders the JTree with checkboxes and
I have a JPanel panel which contains a JTree tree . Sometimes the JTree
I have a JTree with a MouseListener for Right-click detection. When the user clicks,

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.