gpt4 book ai didi

java - 如何在表中以编程方式选择插入的行?

转载 作者:行者123 更新时间:2023-11-30 06:55:28 24 4
gpt4 key购买 nike

如何以编程方式选择插入的表行?该表绑定(bind)到 BeanContainer ,每次单击添加按钮时,我想插入一行并使其无需 ItemClick 即可选择。

我看到了 SQLContainer 的另一个示例 here但它对我不起作用。

下面是成功插入行的按钮的监听器:

addButton.addClickListener(new ClickListener() {            
@Override
public void buttonClick(ClickEvent event) {
Object itemId = addList();
table.addItem(itemId);
table.getItem(itemId).getItemProperty("PS_SECTION").setValue(n);
table.setValue(itemId);
table.select(itemId);
table.commit();
}
});

最佳答案

Select a row programmatically

这是代码:

@Theme("mytheme")
public class MyUI extends UI {

@Override
protected void init(VaadinRequest vaadinRequest) {
final VerticalLayout layout = new VerticalLayout();
layout.setMargin(true);
layout.setSpacing(true);
setContent(layout);

//cache the beans
ArrayList<MyBean> beans = getBeans();

BeanItemContainer container = new BeanItemContainer<>(MyBean.class, beans);

Table table = new Table();
table.setSelectable(true);
table.setImmediate(true);
table.setWidth("200px");
table.setPageLength(5);

table.setContainerDataSource(container);

//select programmatically
table.select(beans.get(1));//this is the key idea! Provide the same bean from cache, for selection.

layout.addComponent(table);
}

@WebServlet(urlPatterns = "/*", name = "MyUIServlet", asyncSupported = true)
@VaadinServletConfiguration(ui = MyUI.class, productionMode = false)
public static class MyUIServlet extends VaadinServlet {
}

public class MyBean {

private int id;
private String field;

public MyBean(int id, String field) {
this.id = id;
this.field = field;
}

public int getId() {
return id;
}

public String getField() {
return field;
}

}

public ArrayList<MyBean> getBeans() {
ArrayList<MyBean> beans = new ArrayList<>();

MyBean bean = new MyBean(1, "Vikrant");
beans.add(bean);

bean = new MyBean(2, "John");
beans.add(bean);

bean = new MyBean(3, "Rahul");
beans.add(bean);

return beans;
}

}

关于java - 如何在表中以编程方式选择插入的行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41933861/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com