0

我有一个 JXTreeTable,它在左侧有一个节点树(不同的数据类型),在右侧有一个网格(默认情况下)。

现在我希望所有显示的值(也是树标题)都取自 getValueAt(row,column) 方法,而不是树节点由它们的 toString() 方法呈现

如何强制 JXTreeTable 不采用特定于类的 toString() 并使用 getValueAt(...) 返回的值?

谢谢!

这是我的模型:

public class TreeTableGroupTreeTableModel extends AbstractTreeTableModel {

    private final static String[] COLUMN_NAMES = { "Tree",
            "Add/Edit", "Remove" };

    //Group-Hierarchy:
    private JXGroup groupRootObject;

    public TreeTableGroupTreeTableModel(JXGroup groupRoot) {

        this.groupRootObject = groupRoot;

    }

// Here is my structure:
//GROUP 1-1 ModuleAggregation 1-* Modules 1-1 JXGroupAccessMemberAggregation 1-* JXGroupAccessSimpleUserObjectLeaf

    /**
     * Returns the value for the node at columnIndex. The node must be managed
     * by this model. Unamanaged nodes should throw an IllegalArgumentException.
     * 
     * 
     * node - the node whose value is to be queried column - the column whose
     * value is to be queried
     */
    @Override
    public Object getValueAt(Object node, int column) {

        System.out.println(node.getClass());

        if (node instanceof JXGroup) {
            switch (column) {
            case 0:
                return "this should render this string instead of the class tostring()";//((JXGroup) node);
            case 1:
            case 2:
                return "";
            }
        } else if (node instanceof JXGroupAccessModuleAggregation) {
            switch (column) {
            case 0:
                return "Modules";
            case 1:
                return "ADD";
            case 2:
                return "REMOVE ALL";
            }
        } else if (node instanceof JXGroupAccessModule) {
            switch (column) {
            case 0:
                return ((JXGroupAccessModule) node).getModule();
            case 1:
                return "ADD";
            case 2:
                return "REMOVE";
            }
        } else if (node instanceof JXGroupAccessMemberAggregation) {
            switch (column) {
            case 0:
                return "Members";
            case 1:
                return "ADD";
            case 2:
                return "REMOVE ALL";
            }
        } else if (node instanceof JXGroupAccessSimpleUserObjectLeaf) {
            switch (column) {
            case 0:
                SimpleUserObject c = FunctionsUser.getUser(
                        metaDataPackage.getUsers(),
                        ((JXGroupAccessSimpleUserObjectLeaf) node).getUserId().getId());
                return c.getShowAs();
            case 1:
                return "";
            case 2:
                return "REMOVE";
            }
        } 

        return null;
    }

    /**
     * Sets the value for the node at columnIndex to value. The node must be
     * managed by this model. Unamanaged nodes should throw an
     * IllegalArgumentException.
     * 
     * value - the new value node - the node whose value is to be changed column
     * - the column whose value is to be changed
     */
    @Override
    public void setValueAt(Object value, Object node, int column) {

        return; // changes are not allowed

    }

    @Override
    public Object getRoot() {
        return groupRootObject;
    }

    @Override
    public int getColumnCount() {
        return COLUMN_NAMES.length;
    }

    @Override
    public String getColumnName(int column) {
        return COLUMN_NAMES[column];
    }

    @Override
    public boolean isCellEditable(Object node, int column) {
        return false;
    }

    @Override
    public boolean isLeaf(Object node) {
        return node instanceof JXGroupAccessCategoryLeaf
                || node instanceof JXGroupAccessChapterLeaf
                || node instanceof JXGroupAccessEntryLeaf
                || node instanceof JXGroupAccessSimpleUserObjectLeaf;
    }

    @Override
    public int getChildCount(Object obj) {

        if (!(obj instanceof JXICountable)) {
            System.out.println("parsing error. should be JXCountable (but is "
                    + obj.getClass() + ")");
            return 0;
        }

        JXICountable obj2 = (JXICountable) obj;

        return obj2.getChildrenCount();

    }

    @Override
    public Object getChild(Object parent, int index) {
        if (parent instanceof JXITreeNodeAble) {
            return ((JXITreeNodeAble) parent).getChild(index);
        }
        throw new IllegalArgumentException(
                "parent is not a member of JXITreeNodeAble");
    }

    @Override
    public int getIndexOfChild(Object parent, Object child) {
        if (parent instanceof JXITreeNodeAble) {
            return ((JXITreeNodeAble) parent).getIndexOfChild(child);
        }
        throw new IllegalArgumentException(
                "parent is not a member of JXITreeNodeAble");
    }
}
4

1 回答 1

1

首先请注意,树表是最复杂的组件之一,因为节点必须保存大量数据:

  • Hierarchichal:给定父子关系。
  • 用户数据:赋予通过setUserObject(..)getUserObject()方法保存和检索用户特定对象的能力。
  • 列数据:每个节点负责检索与每个列索引关联的数据,以便提供以表格形式表示的能力。

此外,所有节点可能保存不同的数据对象,因此问题变得更加复杂。

更新

既然您已经发布了代码及其所基于的代码,请注意实现的目的是没有根(您遇到问题的节点)并直接与域对象一起使用以避免处理TreeTableNodes. 在后续的帖子中,作者修改了它的树表模型实现以包含一个根节点。话虽如此,您至少应该修改两件事:

  1. 不要覆盖getRoot()方法并让实现继承自AbstractTreeTableModel.
  2. 更改您的类构造函数调用适当的super构造函数。

在代码中:

public class TreeTableGroupTreeTableModel extends AbstractTreeTableModel {
    ...
//    private JXGroup groupRootObject; Not needed!
    ...
    public TreeTableGroupTreeTableModel(JXGroup groupRoot) {
        super(groupRoot);
    }
    ...    
//    @Override
//    public Object getRoot() {
//        return groupRootObject;
//    }
    ...
}

虽然这可能有效(如果没有你的所有类,我无法对其进行测试)我想再强调一次,这种实现树表模型的方式对我来说并不合适。我一开始就不会弄乱树表模型,DefaultTreeTableModel而是使用它。我还将为需要在树表中表示的每个域类实现一个树表节点。例如:

class JXGroupAccessModuleTreeTableNode extends AbstractMutableTreeTableNode {

    public JXGroupAccessModuleTreeTableNode(JXGroupAccessModule module) {
        super(module);
    }

    public JXGroupAccessModuleTreeTableNode(JXGroupAccessModule module, boolean allowsChildren) {
        super(module, allowsChildren);
    }

    @Override
    public Object getValueAt(int column) {
        JXGroupAccessModule module = (JXGroupAccessModule)getUserObject();
        switch (column) {
            case 0: return module.getModule();
            case 1: return "ADD";
            case 2: return "REMOVE";
                default: throw new ArrayIndexOutOfBoundsException(column);
        }
    }

    @Override
    public int getColumnCount() {
        return 3;
    }
}

要允许单元格可编辑,您还必须同时覆盖setValueAt(Object value, int column)isEditable(column)方法:默认情况下AbstractMutableTreeTableNodes不可编辑。

按照这种方法,您将获得以下好处:

  • 根本不需要弄乱树表模型。
  • 如果需要更改树结构,您只需根据需要重新排序节点。再次无需修改任何模型的代码。
  • 每个节点的表示与模型分离,因此您可以以不同的方式表示相同的域对象,只需覆盖树表节点(不是模型)中的几个方法。
于 2014-10-29T13:16:33.027 回答