How to use TreeTable
Answered
Hi,
I'm trying to use the TreeTable for my own data. What I have so far is a table looking like this (sorry, struggling to upload screenshot):
I am first name | I am first value
I am second name | I am second value
Using this code:
static ColumnInfo name = new ColumnInfo("Name") {
@Nullable
@Override
public Object valueOf(Object o) {
if (o instanceof CustomNode) {
return ((CustomNode) o).getName();
}
else return o;
}
};
static ColumnInfo value = new ColumnInfo("Value") {
@Nullable
@Override
public Object valueOf(Object o) {
if (o instanceof CustomNode) {
return ((CustomNode) o).getValue();
}
else return o;
}
};
static ColumnInfo[] columns = {name, value};
public static void main(String[] args) {
DefaultMutableTreeNode root = new DefaultMutableTreeNode();
DefaultMutableTreeNode first = new CustomNode("I am first name", "I am first value");
DefaultMutableTreeNode child = new CustomNode("I am child name", "I am child value");
DefaultMutableTreeNode second = new CustomNode("I am second name", "I am second value");
root.add(first);
second.add(child);
root.add(second);
ListTreeTableModel model = new ListTreeTableModel(root, columns);
TreeTable treeTable = new TreeTable(model) {
@Override
public TreeTableCellRenderer createTableRenderer(TreeTableModel treeTableModel) {
TreeTableCellRenderer tableRenderer = super.createTableRenderer(treeTableModel);
UIUtil.setLineStyleAngled(tableRenderer);
tableRenderer.setRootVisible(false);
tableRenderer.setShowsRootHandles(true);
return tableRenderer;
}
@Override
public TableCellRenderer getCellRenderer(int row, int column) {
TreePath treePath = getTree().getPathForRow(row);
if (treePath == null) return super.getCellRenderer(row, column);
Object node = treePath.getLastPathComponent();
@SuppressWarnings("unchecked")
TableCellRenderer renderer = columns[column].getRenderer(node);
return renderer == null ? super.getCellRenderer(row, column) : renderer;
}
@Override
public TableCellEditor getCellEditor(int row, int column) {
TreePath treePath = getTree().getPathForRow(row);
if (treePath == null) return super.getCellEditor(row, column);
Object node = treePath.getLastPathComponent();
@SuppressWarnings("unchecked")
TableCellEditor editor = columns[column].getEditor(node);
return editor == null ? super.getCellEditor(row, column) : editor;
}
};
JTree tree = treeTable.getTree();
tree.setShowsRootHandles(true);
JFrame frame = new JFrame();
frame.setBounds(400, 400, 400, 500);
frame.getContentPane().add(treeTable);
frame.setVisible(true);
}
private static class CustomNode extends DefaultMutableTreeNode {
String name;
Object value;
CustomNode(String name, Object value) {
this.name = name;
this.value = value;
}
public Object getValue() {
return value;
}
public String getName() { return name; }
}
However, my end goal is to have collapsible rows in the table. Currently I'm trying to do so by adding a child node to a non root node, but this doesn't achieve what I want. Please may someone point me in the right direction of how I could get collapsible rows.
Please sign in to leave a comment.
Please try
to see not empty root label and
now you can collapse children rows in root row.
What can I do to unfold it?
I'm afraid you cannot fold it at all as root (parent) node is hidden. And obviously it's impossible to unfold leaf nodes with no children inside.
I have added child nodes