I recently created a flextable using GWT 2.4 using this code:
@uiBinder Flextable flextable;
In the constructor I call setupFlexTable() after I do
initWidget(uiBinder.createAndBindUi(this));
and then the flexTable is instantiated. I missed that part. Using setText() works.
//Header row
private void setupFlexTable() {
flexTable.setText(0, 0, "Label One");
flexTable.setText(0, 1, "Label Two");
}
setWidget didn’t display, but this is what I used:
int numRows = flexTable.getRowCount();
flexTable.setWidget(numRows, 0, new Label("Label One"));
flexTable.setWidget(numRows, 1, new Label("Label Two"));
flexTable.getFlexCellFormatter().setRowSpan(0, 1, numRows + 1);
Here is where I add rows in and this code works with setWidget():
@Override
public void setRowData(Data data) {
int rowCount = flexTable.getRowCount() + 1;
flexTable.setWidget((rowCount), 0, new Label(data.getDataName()));
flexTable.getFlexCellFormatter().setRowSpan(rowCount, 0, 2);
flexTable.setWidget((rowCount), 1, new Label(data.getData()));
flexTable.getFlexCellFormatter().setRowSpan(rowCount, 1, 2);
}
Also, when I use flexTable.setText(“Label One”); etc… it displays, but when I use setWidget(), it does not?
Another issue is I need to keep this table in sync with a list of files above it, but using Firebug I see that if I add two rows dynamically to the table, it shows a rowCount of 4? This throws an index out of bounds error when I try to remove a row and it reaches one of these blank rows.
As a work-around, I use this code to find the row I need to delete and avoid the issue with the blank rows (apparently there are two and only two blank rows inserted in the table).
int rows = flexTable.getRowCount();
for(int i=0; i < rows; i++){
//Match the text in the first field in order to know the proper row to delete.
if(flexTable.isCellPresent(i,0) && flexTable.getText(i, 0).equalsIgnoreCase(fileName)){
flexTable.removeRow(i);
break;
}
}
So can anyone explain why setWidget() doesn’t display anything and why I’m getting an extra blank row for each one I add?
You should not instantiate the FlexTable in your code – GWT does that for you. Once you make a
new FlexTableyou’re working on a new object, NOT the one included in your widget. In the code you’ve posted you have one variable calledflextableand another calledflexTable– it’s easy to imagine some confusion happening.I think your other problems may become more clear once you have this set up properly.