|
Getting to the data |
Top Previous Next |
TreeTableModel builds the tree structure dynamically every time the underlying data changes. To get from the "tree-view" to the original data model provided by the underlying ListTableModel, you can use the following method:
public int getDataRow(int rowIndex);
The above method assumes that rowIndex is a DataRow (single). In order to retrieve the indexes for HeaderRows, use:
public int[] getDataRows(int rowIndex);
public int[] getModelIndexesUnderRow(int row, boolean sorted);
public int[] getModelIndexesUnderRow(TreeTableRow row, boolean sorted);
Furthermore, there are methods for examining the nodes of the tree - the TreeTableRows:
public TreeTableRow getTreeRow(int rowIndex);
public int getLevel(int rowIndex);
public boolean isAggregate(int rowIndex);
public boolean isFooter(int rowIndex);
public boolean isHeader(int rowIndex);
Example 1: Find the objects that correspond to a treetable's row selection. (using the user object of the tree node)
//table is the TreeTable model
int[] selectedRows = table.getSelectedRows();
TreeTableModel ttm = (TreeTableModel) table.getModel();
List treeList = ttm.getRows();
for (int i=0;i<selectedRows.length;i++) {
TreeTableRow treeRow = (TreeTableRow) treeList.get(selectedRows[i]);
Object objectRow = treeRow.getUserObject();
}
Example 2: Find the objects that correspond to a treetable's row selection. (using the underlying ListTableModel)
//table is the TreeTable model
int[] selectedRows = table.getSelectedRows();
TreeTableModel ttm = (TreeTableModel) table.getModel();
ListTableModel wrappedModel = ttm.getModel();
for (int i=0;i<selectedRows.length;i++) {
int origIndex = ttm.getDataRow(selectedRows[i]);
Onject objectRow = wrappedModel.getRows().get(origIndex);
}