import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.ui.Grid;
public class MyGrid extends Grid {
public MyGrid(int i, int j) {
super(i,j);
// use DOM to create thead element....
Element thead = DOM.createElement("thead");
Element tr = DOM.createTR();
// add columns
DOM.appendChild(thead,tr);
for (int k = 0;k<j;k++) {
Element th = DOM.createTH();
DOM.appendChild(tr,th);
// add some text to the header...
DOM.setInnerText(th,"Col#"+k);
}
// get the table element
Element table = this.getElement();
// and add the thead before the tbody
DOM.insertChild(table,thead,0);
}
}
class MyGrid extends Grid {
MyGrid(int row, int col) {
super(row, col);
}
void clearRows() {
resizeRows(1);
}
void insertHeader(String[] fields, String[] styles) {
for (int col = 0; col < fields.length; ++col) {
setText(0, col, fields[col]);
getCellFormatter().setStyleName(0, col, styles[col]);
}
}
void insertData(String[] fields) {
resizeRows(getRowCount()+1);
for (int col = 0; col < fields.length; ++col) {
setText(getRowCount()-1, col, fields[col]);
}
}
//The following method is to fix the Grid Bug.
public void resizeRows(int rows) {
if (numRows == rows)
return;
if (rows <= 0) {
throw new IndexOutOfBoundsException("Cannot set number of rows to"
+ rows);
}
while (numRows < rows) {
// More rows. Add them where necessary.
insertRow(numRows);
insertCells(numRows, 0, numColumns);
++numRows;
}
while (numRows > rows) {
// Fewer rows. Remove extraneous ones.
removeRow(--numRows);
.}
.}
.}.