我正在使用 Vaadin 为单个数据库表构建一个简单的编辑器,使用 JPAContainer 和一个 Vaadin 表组件。删除和编辑项目工作正常,但我在添加项目时遇到问题。
Vaadin 教程解释了如何使用弹出窗口添加项目,但我想通过使用表格中的新空白行来添加项目。所需的功能是为用户提供一些默认数据的行,然后允许用户在对新行中的数据满意后保存数据。
我遇到的问题是,当我尝试做我认为应该工作的事情时,我得到了 UnsupportedOperation 。
我的表已设置并绑定到位置实体的 JPAContainer:
private JPAContainer locations = JPAContainerFactory.make(Location.class,PERSISTENCE_UNIT);
我将表设置为缓冲,这样在用户单击保存按钮之前我不会将数据保存到数据库中:
locationTable = new Table(null,locations);
locationTable.setVisibleColumns(new Object[]{ "id","x","y","z","createDate","lastModDate" } );
locationTable.setSelectable(true);
locationTable.setSizeFull();
locationTable.setImmediate(true);
locationTable.setBuffered(true);
locationTable.setVisible(true);
locationTable.setEditable(true);
.. 保存按钮保存对数据库的更改:
Button saveButton = new Button("Save Changes");
saveButton.addClickListener(new Button.ClickListener() {
public void buttonClick(ClickEvent event) {
locations.commit();
statusLabel.setCaption("Changes Saved");
}
});
然后我绑定一个尝试添加新按钮的按钮。
Button addButton = new Button("Add An Item");
addButton.addClickListener(new Button.ClickListener() {
public void buttonClick(ClickEvent event) {
Object newkey = locationTable.addItem(new Object[]{"NEWLOCATION","0","0","0","7/10/2013","7/10/2013"}, "NEWLOCATION");
locationTable.select(newkey);
statusLabel.setCaption("Item Added. Populate Data and click Save to make permanent.");
}
});
单击 additem 按钮会引发 UnSupportedOperationException。
我曾尝试调用 addItem 的其他变体,但无法正常工作。有人知道如何通过使用表格中的新行在表格中创建新项目吗?
完整的 init() 源代码:
@Override
protected void init(VaadinRequest request) {
final VerticalLayout layout = new VerticalLayout();
layout.setMargin(true);
setContent(layout);
final Label statusLabel = new Label("");
locationTable = new Table(null,locations);
locationTable.setVisibleColumns(new Object[]{ "id","x","y","z","createDate","lastModDate" } );
locationTable.setSelectable(true);
locationTable.setSizeFull();
locationTable.setImmediate(true);
locationTable.setBuffered(true);
locationTable.setVisible(true);
locationTable.setEditable(true);
Button saveButton = new Button("Save Changes");
saveButton.addClickListener(new Button.ClickListener() {
public void buttonClick(ClickEvent event) {
locations.commit();
statusLabel.setCaption("Changes Saved");
}
});
Button addButton = new Button("Add An Item");
addButton.addClickListener(new Button.ClickListener() {
public void buttonClick(ClickEvent event) {
Object newkey = locationTable.addItem(new Object[]{"NEWLOCATION","0","0","0","7/10/2013","7/10/2013"}, "NEWLOCATION");
locationTable.select(newkey);
statusLabel.setCaption("Item Added. Populate Data and click Save to make permanent.");
}
});
Button deleteButton = new Button("Delete Selected");
deleteButton.addClickListener(new Button.ClickListener() {
public void buttonClick(ClickEvent event) {
locations.removeItem(locationTable.getValue());
statusLabel.setCaption("Item Removed. Click Save to make permanent.");
}
});
layout.addComponent(locationTable );
layout.addComponent(saveButton);
layout.addComponent(addButton);
layout.addComponent(deleteButton);
layout.addComponent(statusLabel);
}