3

I have the following problem:

I want to have a JTeeTable-like table component except that the roots (classes below) and the nodes of the tree are not of the same type.

For instance, suppose that I have the following classes:

public final class Entry {

    private int id;
    private String title;
    private String notes;

    private List<SubEntry> subEntryList; /** @see SubEntry*/
}


public final class SubEntry{
    private int id;
    private String title;
    private String notes;

}

Although these two class look similar and may motivate for a case for inheritance, they are NOT related that way. Think of it as a car with a list of car parts OR movie with a list of actors.

For two weeks now I have been trying to think of a way to present the above visually. I thought of using the JTreeTable component but all the examples I've come are cross show usage when the subentries (nodes??) are of the same type as the entries (leaf??).

My Idea So Far: Seeing that if I use a table, the columns will share the same names, I thought I should use a table and another table as a TableCellRenderer and then support show/hide on double-click of the parent row.

I am not sure how to move forward though...

So, if you have any ideas how I should go about this, please share your ideas.

UPDATE

//I finally managed to sort out the tree table model.
//Below is the snippet of what I have done. 

        private List<Entry> root; 

        public EntryTreeTableModel(List<Entry> root) { 
              this.root = root;
        } 

    public int getChildCount(Object parent) { 

      if (parent instanceof List){ 
             return ((ArrayList<Entry>) parent).size(); 
      }else if (parent instanceof Entry){ 
            return ((Entry)parent).getSubEntries().size(); 
      } 
     return 0; 
   }

   public Object getChild(Object parent, int index) {
        if (parent instanceof List){
            return ((ArrayList<Entry>) parent).get(index);
        }else if (parent instanceof Entry){
            return ((Entry)parent).getSubEntries().get(index);
        }else if (parent instanceof Entry){
            return ((SubEntry)parent); // Hmmm, this feels wrong. Check later.
        }
            return "...";  // Just to indicate that something went wrong
    }

Other methods of follow the same approach

Many thanks to all those who shared their ideas. Another thank to Trashgod...

Charles Sprayberry
  • 7,741
  • 3
  • 41
  • 50
user276002
  • 178
  • 3
  • 10
  • I don't really see the problem. Your TreeTableModel (if you are using [this article](http://java.sun.com/products/jfc/tsc/articles/treetable1/)) just has to return the right objects. The ColumnClass for the first column can either be some common supertype of both your node types, or simply Object. – Paŭlo Ebermann Apr 14 '11 at 13:47

1 Answers1

4

The class org.netbeans.swing.outline.Outline appears to be a good candidate, as suggested by this example. In particular, Outline uses javax.swing.tree.TreeModel, which "accepts any kind of object as a tree node." Comparing your model to the example's, Entry would correspond to a directory, and SubEntry would correspond to a list of files contained therein. See Creating a Data Model for a related example.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • ` //I finally managed to sort out the tree table model. Below is the snippet of what I have done. private List root; public EntryTreeTableModel(List root) { this.root = root; } public int getChildCount(Object parent) { if (parent instanceof List){ return ((ArrayList) parent).size(); }else if (parent instanceof Entry){ return ((Entry)parent).getSubEntries().size(); } return 0; } ` – user276002 Apr 24 '11 at 14:21
  • Why not move this code sample to your question? It'll be easier to read. – trashgod Apr 24 '11 at 14:30
  • **Thanks trashgod I have moved the code there to the question. I wondered about a better way to post the solution but I didn't think of that. ** – user276002 Apr 24 '11 at 20:45
  • Looks good. I've up-voted your question, accordingly. Please consider accepting this answer. – trashgod Apr 24 '11 at 21:28
  • Click the gray check mark to the left. See the [faq](http://stackoverflow.com/faq) for more details. – trashgod Apr 25 '11 at 12:29