开发者

Using TreeTable with different root and node types

开发者 https://www.devze.com 2023-02-24 11:26 出处:网络
I have the f开发者_开发知识库ollowing 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.

I have the f开发者_开发知识库ollowing 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...


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号