使用具有不同根和节点类型的TreeTable

使用具有不同根和节点类型的TreeTable

本文介绍了使用具有不同根和节点类型的TreeTable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到以下问题:

我想要一个类似JTeeTable的表组件,除了根(下面的类)和树的节点类型不同。

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.

两周以来我一直在尝试想办法在视觉上呈现上述内容。我想过使用JTreeTable组件,但是当子条目(节点??)与条目(leaf ??)的类型相同时,我所有的例子都是交叉显示用法。

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??).

我的想法到目前为止:
看到如果我使用表格,列将共享相同的名称,我想我应该使用表格和另一个表格作为TableCellRenderer然后在双击父行时支持显示/隐藏。

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.

更新

//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
    }

其他跟进方法同样的方法


推荐答案

班级似乎是一个很好的候选人,正如。特别是,使用,将任何类型的对象接受为树节点。将您的模型与进行比较, Entry 将对应于一个目录, SubEntry 将对应于其中包含的文件列表。请参阅 创建数据模型 相关的例子。

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.

这篇关于使用具有不同根和节点类型的TreeTable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 04:19