If I expand and collapse the rows in opposite order the expansion is ok.
If i use a different expansion/collapse row order, the tree table reach an inconsistent state.
For example I expand the first row, and then the second row.
all the childs seems to be appended to the first row.
Then if I try to play with expansion collapse of the row some first level rows disappear.
the code to play is:
- Code: Select all
import java.awt.Dimension;
import java.util.*;
import javax.swing.*;
import com.jidesoft.grid.*;
import com.jidesoft.swing.JideSwingUtilities;
public class TreeTableTest {
public static void main(String[] args) {
JFrame f = new JFrame("Table test!");
MyTableModel model = new MyTableModel();
initModel(model);
TreeTable treeTable = new TreeTable(model);
treeTable.setExpandAllAllowed(false);
treeTable.setRowHeight(18);
treeTable.setShowTreeLines(true);
treeTable.setShowGrid(false);
treeTable.setIntercellSpacing(new Dimension(0, 0));
f.setContentPane(new JScrollPane(treeTable));
f.pack();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JideSwingUtilities.globalCenterWindow(f);
f.setVisible(true);
}
protected static void initModel(MyTableModel model) {
ArrayList<ExpandableRow> rows = new ArrayList<ExpandableRow>();
for (int i = 0; i < 3; i++) {
Object[] rowData = { "name " + i };
rows.add(new MyElementRow(i + 1, rowData, false));
}
model.setOriginalRows(rows);
}
public static class MyTableModel extends TreeTableModel {
private String[] columnNames;
public MyTableModel() {
columnNames = new String[] { "name", "prop1", "prop2", "prop3" };
}
@Override
public String getColumnName(int column) {
if (column >= 0 && column < columnNames.length)
return columnNames[column];
return null;
}
public int getColumnCount() {
return columnNames.length;
}
}
public static class MyElementRow extends DefaultExpandableRow {
protected Object[] rowValues;
protected long childs;
Random r = new Random();
public MyElementRow(long childs, Object[] values, boolean leafRow) {
this.childs = childs;
this.rowValues = values;
setExpandable(!leafRow);
if (!leafRow) {
ArrayList<Row> list = new ArrayList<Row>();
for (int i = 0; i < childs; i++) {
Object[] rowData = { "", r.nextInt(20), r.nextBoolean(), r.nextInt(20) };
Row row = new MyElementRow(i, rowData, true);
row.setParent(this);
list.add(row);
}
setChildren(list);
}
}
public Object getValueAt(int columnIndex) {
if (rowValues != null && columnIndex >= 0 && columnIndex < rowValues.length)
return rowValues[columnIndex];
return null;
}
public boolean hasChildren() {
return isExpandable() && getChildrenCount() > 0;
}
}
}
I wrote wrong code or there are other problems?
Thanks