0

I am working on a TreeTable component of a Swing application. I followed the tutorial given at TreeTables2

I am passing a Message object to the PropertiesTableModel which I used to create the JTreeTable and suppose to view them as shown in the bellow image.

Tree table design

following is the source code of my table model.

import javax.swing.*;
import java.util.List;

public class PropertiesTableModel extends AbstractTreeTableModel    implements TreeTableModel{

private List<Field> fieldList;
private static int fiedlCount=0;

// Names of the columns.
static protected String[] columnNames= 
new String[]{"Field","Data Type","Value"};
// Types of the columns.
static protected Class[] columnTypes = { TreeTableModel.class,
        ImageIcon.class, Object.class};

public PropertiesTableModel(Message msg){
    super(msg);
    this.fieldList=msg.getListFields();
}

//  The TreeTableNode interface.
@Override
public int getColumnCount() {
    return columnNames.length;
}

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

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

public Object getValueAt(Object node,int column) {
    Message msg=(Message)node;

    if (fiedlCount < fieldList.size()){
        Field field = msg.getListFields().get(fiedlCount);
        fiedlCount++;

        if(column==0){
            return field.getFieldDef().getfName();
        }

        if(column==1){
            String dataType=field.getFieldDef().getDataType();
            return PropertiesPanel.getPpIns().getDataTypeIcon(dataType);
        }

        else if (column==2){
            if(field.getFieldDef().getListValue().size()>0){
                return field.getFieldDef().getListValue();
            }
            else{
                return field.getDefaultValue();
            }
        }
    }
    return null;
}
// The TreeModel interface
public int getChildCount(Object node) {
    Object[] children = getChildren(node);
    return (children == null) ? 0 : children.length;
}

public Object getChild(Object node, int i) {
    return getChildren(node)[i];
}

public boolean isLeaf(Object node) {
    return true;
}

protected Object[] getChildren(Object node) {
    return null;
}

}

When I run the app, only the root node (Message) gets appear in the Table.

enter output

This is the first time I am trying out a customized Tree table component in swing and I feel it bit complex. Can someone please help me to view the data accordingly in the Tree table. Or if someone has a better workaround than doing this approach in order to build a Tree Table easier than this, please help me out.

sher17
  • 607
  • 1
  • 12
  • 31

2 Answers2

0

You always return true when isLeaf is called. That means that you are saying that the node has no children.

LarsErik
  • 138
  • 1
  • 7
0

I found a good article on how to use JXTreeTable to implement a Tree Table easily. It's straightforward that the approach followed in Swing since SwingX already provides a component for this. I just had to implement a TreeTableModel to suite my requirements.

sher17
  • 607
  • 1
  • 12
  • 31