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

  • Home
  • SEARCH
  • 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 645325
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T21:29:22+00:00 2026-05-13T21:29:22+00:00

How I can implement dynamic Jtree, which shows created instances of clases? For example,

  • 0

How I can implement dynamic Jtree, which shows created instances of clases?

For example, I can create new Book(name) in my app. In every book can be chapters = ArrayList of Chapter. And now How I can do a jtree from it?

  • 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-13T21:29:22+00:00Added an answer on May 13, 2026 at 9:29 pm

    Class involved in JTree usage are the following:

    • the JTree class itself which provides the displayable item you need and it works exactly like tables and lists in swing: they have a model!
    • the DefaultTableModel implements TableModel which works as a data container for a JTree. It is istantiated with a root node, then every children is added to root node or to other nodes contained in the tree.
    • the DefaultMutableTreeNode which is a general purpose default implementation for a generic JTree node.

    How to mix up these things? First of all I suggest you to check java sun guide about it here but to have a quick look you can think about having something like this:

    // this is the root of your tree
    DefaultMutableTreeNode root = new DefaultMutableTreeNode("Books");
    
    for (Book b : books)
    {
      // this adds every book to the root node
      DefaultMutableTreeNode curBook = new DefaultMutableTreeNode(b);
    
      root.add(curBook);
    
      // this adds every chapter to its own book
      for (Chapter c : b.chapters())
        curBook.add(new DefaultMutableTreeNode(c));
    }
    
    // at this point you have your tree ready, you just have to setup the model and create the effective JTree
    
    DefaultTreeModel treeModel = new DefaultTreeModel(root);
    JTree tree = new JTree(treeModel);
    
    //now you tree is ready to be used
    

    Approach is really identical to the one you use for JTable or JList also if data structure (and so model) differs. Think about that this is the default way to do it but you can easily write your own TreeNode or TreeModel class according to what you really need.

    I would like to let you know that sun’s guide about java contains almost every topic contained in the basic JDK so it’s a good thing to give it a look before feeling lost.

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

Sidebar

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.