Search code examples
javaswingswingxjxtreetablejtreetable

JXTreeTable model doesn't allow to use insertNodeInto()


I am using a tree table model in my app which extends AbstractTreeTableModel in order to create a JXTreeTable. Below is my model.

import org.jdesktop.swingx.treetable.AbstractTreeTableModel;
import org.jdesktop.swingx.treetable.DefaultTreeTableModel;
import org.jdesktop.swingx.treetable.TreeTableModel;

import javax.swing.tree.TreeModel;

public class MyDataModel extends AbstractTreeTableModel{

    static protected String[] columnNames = { "Field", "Value" };
    static protected Class<?>[] columnTypes = { Object.class, Object.class};

    public MyDataModel(MyDataNode rootNode) {
        super(rootNode);
        root = rootNode;
    }

    @Override
    public Object getChild(Object parent, int index) {
        return ((MyDataNode) parent).getChildren().get(index);
    }

    @Override
    public int getChildCount(Object parent) {
        return ((MyDataNode) parent).getChildren().size();
    }

    @Override
    public int getIndexOfChild(Object parent, Object child) {
        return 0;
    }

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

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

    @Override
    public Class<?> getColumnClass(int column) {
        return columnTypes[column];
    }

    @Override
    public Object getValueAt(Object node, int column) {
        MyDataNode mNode=(MyDataNode)node;
        Object obj =mNode.getNodeDataObject();

        if(column==0){
            return mNode.getName();
        }
        else if (column==1){
            if(obj instanceof Field){
                Field field=(Field)mNode.getNodeDataObject();
                if(field.getFieldDef().getListValue().size()>0){
                    return field.getFieldDef().getListValue();
                }
                else
                    return mNode.getDefaultValue();
            }
            else
                return mNode.getDefaultValue();
        }
        return null;
    }

    @Override
    public boolean isCellEditable(Object node, int column) {
        //only allow Field values to be editable
        if(((MyDataNode)node).getNodeDataObject() instanceof Field && column==1)
            return true;
        else
            return false;
    }

    @Override
    public boolean isLeaf(Object node) {
        MyDataNode mNode=(MyDataNode)node;
        Object obj=mNode.getNodeDataObject();

        if(obj instanceof Field){
            Field field=(Field)obj;
            if(field.getFieldDef().getDataType().equalsIgnoreCase("MULTIPLE_MESSAGE")){
                return false;
            }
            else
                return true;
        }

        return false;
    }

    @Override
    public void setValueAt(Object aValue, Object node, int column) {
        MyDataNode mNode=(MyDataNode)node;

        if (mNode.getNodeDataObject() instanceof Field && column == 1) {
            Field field = (Field) mNode.getNodeDataObject();
            field.setDefaultValue(aValue);
            field.setSelectedValue(aValue);
        }
    }


}

This is how I use the JXTreeTable in my app

    MyDataModel treeTableModel = new MyDataModel(createDataStructure(message));
    jTreeTable = new JXTreeTable(treeTableModel);

    private static MyDataNode createDataStructure(Message message) {

    //setting fields as children of the root
    nodeList = new ArrayList<>();

    for (int index=0;index<message.getListFields().size() ; index++) {
        if(message.getListFields().get(index).getFieldDef()
        .getDataType().equalsIgnoreCase("MULTIPLE_MESSAGE")){
            nodeList.add(new MyDataNode(message.getListFields()
            .get(index).getFieldDef().getfName(), "", childMessagesRoot,
            message.getListFields().get(index)));
        }
        else{
            nodeList.add(new MyDataNode(message.getListFields()
            .get(index).getFieldDef().getfName(), (String)(message.getListFields().
            get(index).getDefaultValue()),
            null,message.getListFields().get(index)));
        }
    }
    //setting the Message to the root of the tree
    root = new MyDataNode(message.getMsgName(), "", nodeList,message);
    return root;
}

when I need to add a new node to the JXTreeTable, I try to get its model and use insertNodeInto() function call but the model doesn't support the insertNodeInto() function.

Someone please let me know where I am going wrong in the code. This is the first time I am using tree tables so there could be something missing.


Solution

  • insertNodeInto is a method of DefaultTreeTableModel, but not one of AbstractTreeTableModel.

    Let MyDataModel extend DefaultTreeTableModel rather than AbstractTreeTableModel to be able to use insertNodeInto.

    When accessing the TreeTableModel through JXTreeTable#getTreeTableModel, remember to cast the returned object of type TreeTableModel to DefaultTreeTableModel before calling insertNodeInto.