我是Vaddin的新手。 如何在Vaadin表中默认选中的每一行中选中复选框? 我已经通过一些Vaadin示例,通过编写“field.setValue(true)”来指定将在每行中选中复选框。但解决方案对我不起作用。请帮我找出下面代码我出错的地方。在这里,我使用DefaultFieldFactory为每一行生成字段。
public class ReviewTimesheetTable extends ViewComponent {
private Map<ReviewTimesheetTableDto, HashMap<String, AbstractField<?>>> tableItem = new HashMap<ReviewTimesheetTableDto, HashMap<String, AbstractField<?>>>();
BeanItemContainer<ReviewTimesheetTableDto> data = new BeanItemContainer<ReviewTimesheetTableDto>(
ReviewTimesheetTableDto.class);
private Table table;
public void init() {
// Create a data source and bind it to a table
table = new Table("", data);
table.addStyleName("generateColumnTable");
table.setWidth("100%");
table.setPageLength(table.getItemIds().size());
table.setVisibleColumns(new Object[] { "chkBox", "taskDate", "employeeId", "taskId", "crJiraId",
"taskDesc","hour","minute"/*, "Delete" */});
table.setColumnHeader("chkBox", "Select");
table.setColumnHeader("taskDate", "Task Date");
table.setColumnHeader("employeeId", "Employee ID");
table.setColumnHeader("taskId", "Task ID");
table.setColumnHeader("crJiraId", "Subtask ID");
table.setColumnHeader("taskDesc", "Task Description");
table.setColumnHeader("hour", "Hour");
table.setColumnHeader("minute", "Minute");
table.setEditable(true);
table.setTableFieldFactory(new ImmediateFieldFactory());
}
public class ImmediateFieldFactory extends DefaultFieldFactory {
@Override
public Field<?> createField(Container container, Object itemId,
Object propertyId, Component uiContext) {
final ReviewTimesheetTableDto timesheetDetail = (ReviewTimesheetTableDto) itemId;
Map<String, AbstractField<?>> tableRow = null;
if(tableItem.get(timesheetDetail) == null)
{
tableItem.put(timesheetDetail,
new HashMap<String, AbstractField<?>>());
}
tableRow = tableItem.get(timesheetDetail);
if ("taskId".equals(propertyId)) {
GComboBox box = new GComboBox();
tableRow.put("taskId", box);
setTaskValues(box, timesheetDetail);
//addTaskListener(box);
box.setData(timesheetDetail);
if(timesheetDetail != null && timesheetDetail.getTaskId() != null) {
box.setDescription(timesheetDetail.getTaskId().getValue());
}
if(isSubmit){
box.setReadOnly(true);
}
if(timesheetDetail.getIsAdd() != null && timesheetDetail.getIsAdd()){
box.setReadOnly(false);
}
return box;
}else if ("crJiraId".equals(propertyId)) {
TextField field = new TextField();
field.setEnabled(true);
field.setWidth("200px");
field.setNullRepresentation("");
if(isSubmit){
field.setReadOnly(true);
}
if(timesheetDetail.getIsAdd() != null && timesheetDetail.getIsAdd()){
field.setReadOnly(false);
}
field.setMaxLength(50);
field.setDescription(timesheetDetail.getCrJiraId());
tableRow.put("crJiraId", field);
return field;
}else if ("taskDesc".equals(propertyId)) {
TextField field = new TextField();
field.setEnabled(true);
field.setWidth("300px");
field.setNullRepresentation("");
field.setReadOnly(false);
tableRow.put("taskDesc", field);
field.setMaxLength(2000);
field.setData(timesheetDetail);
addDetailPopupForTaskDesc(field, null);
if(timesheetDetail.getTaskDesc() != null){
field.setDescription(timesheetDetail.getTaskDesc());
}
field.setReadOnly(true);
if(timesheetDetail.getIsAdd() != null && timesheetDetail.getIsAdd()){
field.setReadOnly(false);
}
return field;
}else if ("hour".equals(propertyId)) {
GComboBox box = new GComboBox();
box.setWidth("60px");
tableRow.put("hour", box);
box.addValueChangeListener(addHourListener(box));
setHourValues(box, timesheetDetail);
box.setData(timesheetDetail);
if(isSubmit){
box.setReadOnly(true);
}
if(timesheetDetail.getIsAdd() != null && timesheetDetail.getIsAdd()){
box.setReadOnly(false);
}
return box;
}else if ("minute".equals(propertyId)) {
GComboBox box = new GComboBox();
box.setWidth("80px");
tableRow.put("minute", box);
box.addValueChangeListener(addMinuteListener(box));
setMinuteValues(box, timesheetDetail);
box.setData(timesheetDetail);
if(isSubmit){
box.setReadOnly(true);
}
if(timesheetDetail.getIsAdd() != null && timesheetDetail.getIsAdd()){
box.setReadOnly(false);
}
return box;
}else if ("employeeId".equals(propertyId)) {
/*TextField field = new TextField();
field.setEnabled(true);
field.setWidth("100%");
field.setNullRepresentation("");
field.setReadOnly(true);
field.setMaxLength(50);
field.setDescription(timesheetDetail.getEmpId());
tableRow.put("empId", field);
return field;*/
GComboBox box = new GComboBox();
tableRow.put("employeeId", box);
setEmpValues(box, timesheetDetail);
box.setData(timesheetDetail);
if(timesheetDetail != null && timesheetDetail.getEmployeeId() != null) {
box.setDescription(timesheetDetail.getEmployeeId().getValue());
}
if(isSubmit){
box.setReadOnly(true);
}
if(timesheetDetail.getIsAdd() != null && timesheetDetail.getIsAdd()){
box.setReadOnly(false);
}
/*if(timesheetDetail.getIsAdd() != null && timesheetDetail.getIsAdd()){
box.setReadOnly(false);
}*/
return box;
}else if ("taskDate".equals(propertyId)) {
DateField field = new DateField();
field.setEnabled(true);
field.setWidth("110px");
field.setReadOnly(true);
if(timesheetDetail.getIsAdd() != null && timesheetDetail.getIsAdd()){
field.setReadOnly(false);
}
field.setDateFormat("dd-MM-yyyy");
tableRow.put("taskDate", field);
return field;
}else if ("chkBox".equals(propertyId)) {
CheckBox field = new CheckBox();
field.setEnabled(true);
field.setWidth("30px");
field.setDescription(SHAConstants.SELECT);
**field.setValue(true);**
tableRow.put("chkBox", field);
return field;
}else {
Field<?> field = super.createField(container, itemId,
propertyId, uiContext);
if (field instanceof TextField)
field.setWidth("100%");
field.setEnabled(true);
return field;
}
}
}