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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T02:56:39+00:00 2026-06-08T02:56:39+00:00

I am trying to write a class that implements the TreeModel class. I was

  • 0

I am trying to write a class that implements the TreeModel class. I was hoping someone could guide me in the right direction. Below is my class. The problem is when I bind it to a jTree Component, the second level keeps being added over and over. So I suspect my reference to the parent object is wrong:

public class PMEntry implements TreeModel{

private String title;
private List<PMEntry> pmEntryCollection;
private String pmId;
private String href;
private PMEntry root;
private ModuleType type;

public PMEntry (PMEntry root){

  this.root = root;
}

@Override
public Object getRoot() {        

   return ((PMEntry)this.root);
}

@Override
public Object getChild(Object o, int i) {


    if(getPmEntryCollection().isEmpty()){

        return null;

    }else {

      return (PMEntry) getPmEntryCollection().get(i);

    }
}

@Override
public int getChildCount(Object o) {

   if(getPmEntryCollection().isEmpty()){

        return 0;

    }else {

      return getPmEntryCollection().size();

    }
}

@Override
public boolean isLeaf(Object o) {
    PMEntry pmentry = (PMEntry)o;
    return (pmentry.getType() == ModuleType.DM) ? true : false;
 }

@Override
public void valueForPathChanged(TreePath tp, Object o) {
   //todo
}

@Override
public int getIndexOfChild(Object parent, Object child) {

    if (!(parent instanceof PMEntry)){

        System.out.println("Returning -1");
        return -1;
    }           

    PMEntry pParent = (PMEntry) parent;

    List<PMEntry> children = pParent.getPmEntryCollection();

    if (children == null) {
        System.out.println("children = null, Returning -1");
        return -1;

    }

    for (int i = 0; i < children.size(); i++) {

        System.out.println("Child:" + child);

        if (children.get(i) == child) {

            return i;
        }

    }

    return -1;        
}

@Override
public void addTreeModelListener(TreeModelListener tl) {
   //todo
}

@Override
public void removeTreeModelListener(TreeModelListener tl) {
    //todo
}

@Override
public String toString(){

    return this.getTitle();
}
public enum ModuleType {

    PM,
    DM

}

// getters and setters here....

And here is a snippet of how I am binding the data

PMEntry tm = new PMEntry(null);
tm.setTitle("Root");

PMEntry pmRoot = new PMEntry((PMEntry)(tm));
pmRoot.setTitle("Project");

PMEntry pm1 = new PMEntry(pmRoot);
pm1.setType(PMEntry.ModuleType.DM);
pm1.setTitle("Publication Module");

PMEntry pm2 = new PMEntry(pmRoot);
pm2.setType(PMEntry.ModuleType.PM);
pm2.setTitle("Chapter");     

List<PMEntry> pmCollection = new ArrayList<PMEntry>();      
List<PMEntry> pmCollection1 = new ArrayList<PMEntry>();

PMEntry pm3 = new PMEntry(null);
pm3.setType(PMEntry.ModuleType.DM);
pm3.setTitle("Data Module");

PMEntry pm4 = new PMEntry(null);
pm4.setType(PMEntry.ModuleType.DM);
pm4.setTitle("Data Module");

pmCollection1.add(pm3);
pmCollection1.add(pm4); 

pm2.setPmEntryCollection(pmCollection1);

pmCollection.add(pm1);
pmCollection.add(pm2);                     

pmRoot.setPmEntryCollection(pmCollection);

this.jTree1.setModel(pmRoot);
  • 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-08T02:56:40+00:00Added an answer on June 8, 2026 at 2:56 am

    I’d wonder why you think you need to implement TreeModel. Have you looked into DefaultTreeModel? What new behavior over and above that class do you plan to implement?

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

Sidebar

Related Questions

I'm trying to write a class that implements 64-bit ints for a compiler that
I am trying to write a class that implements a tree using an array
I am trying to write class that inherits from FMX TStyledControl. When style is
I'm trying to write a class that will return a string of HTML with
I've been trying to write an error handling class that I can use on
I am trying to write a Unit test for a class that has several
I'm trying to write a code that adds a class to a div for
I'm trying to write a named scope that will order my 'Products' class based
I'm trying to write a class that has a generic member variable but is
I've been trying to write a program in R that implements Newton's method. I've

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.