Adapts a TreeModel to a TreeTableModel. The per-node columnar data is provided by a NodeModel. If the columnar data should be editable, a NodeChangedMediator is required to handle the change notification in the adapted TreeModel.
Example usage: Assuming a default tree model with build from treeNodes which have Person objects as userObject, developers can implement a PersonNodeModel for the columnar data
public class PersonNodeModel implements NodeModel { public Class<?> getColumnClass(int column) { if (column == 2) return Date.class; return String.class; } public int getColumnCount() { return 3; } public String getColumnName(int column) { switch(column) { case 0: return "Name"; case 1: return "First Name"; case 2: return "Birth Date"; default: return null; } } public int getHierarchicalColumn() { return 0; } public Object getValueAt(Object node, int column) { Person person = getPerson(node); if (person == null) return note; switch (column) { case 0: return person.getFamilyName(); case 1: return person.getFirstName(); case 2: return person.getBirthDate(); } return null; } private Person getPerson(Object node) { if (node instanceof DefaultMutableTreeNode) { node = ((DefaultMutableTreeNode) node).getUserObject(); } if (node instanceof Person) return (Person) node; return null; } public boolean isCellEditable(Object node, int column) { return true; } public void setValueAt(Object value, Object node, int column) { Person person = getPerson(node); if (note == null) return; switch(column) { case 0: person.setFamilyName(String.valueOf(value)); break; case 1: person.setFirstName((String) value); break; case 2: person.setBirthDate((Date) value); } } }
For usage in a read-only JXTreeTable:
TreeTableModel treeTableModel = new TreeTableModelAdapter(personTreeModel, new PersonNodeModel()); treeTable.setModel(treeTableModel);
For usage in an editable JXTreeTable, you'll need to configure the adapter with an appropriate NodeChangedMediator which takes care of change notification by the adapted TreeModel. For TreeModels of type DefaultTreeModel, there's a pre-defined default mediator (for custom types you'll have to implement a custom mediator which knows how to trigger changed events on behalf of the TreeModel):
treeTableModel.setNodeChangedMediator(NodeChangedMediator.DEFAULT);