This chapter describes the Cells Vector in detail and describes how to allocate and manipulate the Vector.
setCells((JCVector)null);When JClass LiveTable Pro draws or redraws each cell,1 it retrieves the cell value from the Cells Vector pointed to by getCells() (this pointer and the Vector it points to should be considered read-only). If there is no Vector, or no value exists in the Vector for the cell, JClass LiveTable Pro draws a blank cell. 2
As a program or a user enters cell values, JClass LiveTable Pro automatically updates and maintains its Cells Vector.
This Vector is not tied to the table's attributes. Cells can be set to display Cells Vectors without affecting the attributes displayed (such as cell background colors and fonts). The next figure illustrates the separation of cell values from the table.
addColumns(), addRows() | Inserts new rows/columns into the table's Cells Vector and increases NumRows and NumColumns. Keeps the attributes of existing rows/columns with their cell values. |
deleteColumns(), deleteRows() | Removes rows/columns and their attributes from the table's Cells Vector. Decreases NumRows and NumColumns. |
moveColumns(), moveRows() | Moves rows or columns within the table's Cells Vector. Keeps the attributes of existing rows/columns with their cell values. |
sortbyColumn() | Sorts rows in the table Cells Vector; the String equivalents of any class based on Numeric cell values. Calling this method has no effect on the spanned ranges, cell properties or selected cells of the table, as only the values stored in the table are changed. After the table has been sorted, SORT is called. |
Information can also be retrieved by using a separate, non-HTML data file. This topic is covered in Chapter 6.
public static JCVector read(Component comp, String file, char delim, boolean convert)
Its parameters include:
Note: You should set NumRows and NumColumns to the size of the cell values Vector when using readParseData() because the cell values Vector is independent of the number of rows/columns in the table. The following code shows how to do this:
JCVector cells = JCFile.read(this, URL, ',', true, true); table.setCells(cells); // Set number of rows and columns int last_column = 0; for (int i = 0; i < cells.size(); i++) { JCVector row = (JCVector)cells.elementAt(i); if (row == null) continue; last_column = Math.max(last_column, row.size()); } table.setNumColumns(last_column); System.out.println("# rows = " + cells.size()); table.setNumRows(cells.size()); table.setPixelWidth(JCTblEnum.ALL, JCTblEnum.ALL, JCTblEnum.VARIABLE); table.setPixelHeight(JCTblEnum.ALL, JCTblEnum.ALL, JCTblEnum.VARIABLE);
The last two lines in the previous example set the cell sizes to the size required by the data in the cell. This guarantees that the data will be visible.
JCFile provides other methods for loading data into a String (readLines()) or an array of Strings (read()). JCURLDirectory provides a method to retrieve a listing of URLs contained in a directory (getFileList()).
JCVector tempcells = table.getCells(); if (tempcells.size() < 2) { System.out.println("No value at (2,3) - row missing"); } else { JCVector row = (JCVector)tempcells.elementAt(1); if (row.size() < 3) { System.out.println("No value at (2,3) - column missing"); } else { System.out.println("Value at (2,3) is " + row.elementAt(2).toString()); } }or
Object obj = table.getCell(1, 2); System.out.println("Value at (2,3) is " + obj.toString());Use getCell() for retrieving individual cell values. Use getCells() if the Vector of Vectors of cells is going to be used in other ways (for example, to modify cell values).
By default, cell values are retrieved as an Object. Use Java's built-in run-time type-checking mechanism to convert to a more appropriate type. For example:
Object obj=table.getCell(1, 2) if(obj instanceofJCString); JCString j=(JCString)obj;
public boolean SearchTable(String find_it) { int row = getNumRows(); int col = getNumColumns(); for (int cnt = 0; cnt < row; cnt++) for (int cnt2 = 0; cnt2 < col; cnt2++) if (find_it.equals(getCell(cnt,cnt2))) { System.out.println("Found it!!"); return true; } return false; }
Call this function with a statement like:
Table.SearchTable(new String("Find me"));
When using addColumns() and addRows(), note the following:
The prototype for the addColumns() instance method is as follows:
public boolean addColumn(int position, Object label, Vector values)
The position parameter is the initial column index, and new columns are added prior to this position. If this is set to JCTblEnum.MAXINT, the column is added after the final column. The label parameter refers to the column label, and values to the array of objects to be inserted. Both the labels and values parameters can be set to null. addColumn() returns false if any of the arguments are invalid.
The following example inserts five columns before column zero, with the first column populated with three values:
String va[] = { new String("a"), new String("b"), new String("c") }; JCVector values = new JCVector(va); // Add first column with values table.addColumn(0, null, values); // Add other four columns table.addColumn(1, null, null); table.addColumn(1, null, null); table.addColumn(1, null, null); table.addColumn(1, null, null);
When using deleteColumns() and deleteRows(), note the following:
The prototype for the deleteColumns() instance method is as follows:
public boolean deleteColumns(int pos, int num_columns)Its parameters include pos, which designates the first column number to be deleted, and num_columns, which is the number of columns to be deleted. deleteColumns() returns false if any of the arguments are invalid.
The following call deletes columns 2 through 5, and shifts the column labels up along with the columns:
table.deleteColumns(1, 4);
The source or destination row or column cannot be greater than the corresponding NumRows or NumColumns value.
If the current cell is at or below any affected row or column, any cell editing is cancelled, and the Text component is unmapped.
The prototype of the moveColumns() instance method is as follows:
public boolean moveColumns(int source, int num_columns, int destination)Its parameters include source, which is the first column in the range, num_columns, which is the number of columns to move, and destination, the column number the moved columns are placed immediately before (i.e. this column number minus one column is where the moved columns are placed). moveColumns() returns false if any of the arguments are invalid.
The following call moves rows 4 through 6 to start before the first row, without shifting the labels:
table.moveRows(3, 3, 0);
You can clear individual cell values using setCell(). The following example clears the cell at row 5, column 2:
setCell(4, 1, null);To clear a range of cells, try something similar to the code below, which clears a block from row 4 to row 6, from column 2 to column 4:
JCVector cells = table.getCells(); for (int row = 3; row < 6; row++) { for (int col = 1; col < 4; col++) { ((JCVector)cells.elementAt(row)).setElementAt(col, null); } } table.setCells(cells);Alternatively, setRepaint() can also be used to minimize any repaints that may occur when using setCell():
table.setRepaint(false); for (int row = 3; row < 6; row++) { for (int col = 1; col < 4; col++) { table.setCell(row, col, null); } } table.setRepaint(true);
To clear the table of all cell values, set clearCells(). The NumRows and NumColumns properties do not change, and any attributes defined for the table remain in effect.
1 JClass LiveTable Pro draws cells as a result of scrolling or other expose events.
2 If a cell value method has been defined, the table calls that method for the value to display for blank cells.