| ArrayGrid is an implementation of the Grid interface which provides a disconnected dataset.
The samples below populate an ArrayGrid with values from an
array and then print out the values in the ArrayGrid.
Notes
- Don't add the same Column to an ArrayGrid twice. The following code illustrates the error.
Grid ag = new ArrayGrid();
Grid.Column col = new ArrayGrid.Column();
ag.getColumns().add(col);
ag.getColumns().add(col); //!!! Don't do this.
- Don't add the same Row to an ArrayGrid twice.
Grid ag = new ArrayGrid();
Grid.Row row = new ArrayGrid.Row();
ag.getRows().add(row);
ag.getRows().add(row); //!!! Don't do this.
- If you replace a Row in an ArrayGrid, be careful with the replaced Row.
If after replacing a Row, you retrieve an element from the replaced Row, the element
will still be converted according to the ArrayGrid's policy.
Grid ag = new ArrayGrid();
Grid.Row row1 = new ArrayGrid.Row();
ag.getRows().add(row1);
Grid.Row row2 = new ArrayGrid.Row();
Grid.row row3 = ag.getRows().set(0, row2); //Be careful with row3.
Object rowVal = row3.get(0); //Be careful with rowVal.
In the code above, row1 is replaced in ag with row2, and row3 is identical to
row1. Despite its removal from ag, row1 (aka row3) still has ag as a parent. If you retrieve
an element from row1 (aka row3), the value you retrieve may not be the exact Object placed in
row1. The value you retrieve - e.g. rowVal in the code above - is a value converted from the
original via ag's policy.
Sample Code:
int[][] values = new int[][]{ { 0, 1, 2, 3, 4 }, { 10, 11, 12, 13, 14 },
{ 20, 21, 22, 23, 24 }, { 30, 31, 32, 33, 34 }, { 40, 41, 42, 43, 44 } };
ArrayGrid ag = new ArrayGrid();
for (int i = 0; i < 5; i++) {
ArrayGrid.Column col = new ArrayGrid.Column();
ag.getColumns().add(col);
}
for (int i = 0; i < 5; i++) {
ArrayGrid.Row row = new ArrayGrid.Row();
ag.getRows().add(row);
for (int j = 0; j < 5; j++) {
row.set(j, new Integer(values[i][j]));
}
}
System.out.println("\r\n");
for (int i = 0; i < 5; i++) {
ArrayGrid.Row row = (ArrayGrid.Row) ag.getRows().get(i);
String line = "";
for (int j = 0; j < 5; j++) {
line += row.get(j) + " ";
}
System.out.println(line);
}
author: Joshua J. Gertzen |