【问题标题】:How to set the DataType of a Vaadin7 Grid after it was created创建后如何设置 Vaadin7 Grid 的 DataType
【发布时间】:2019-01-21 09:33:09
【问题描述】:

我正在使用 Vaadin-7 设计器创建一个应该包含多个列的网格,其中一些不是字符串。

当我尝试添加包含非字符串元素的行时,我收到错误:

java.lang.IllegalArgumentException: Parameter 0(4711) is not an instance of java.lang.String
at com.vaadin.ui.Grid.addRow(Grid.java:6821)

如何向 Grid 提供 Column 应为 Integer 的信息?

由于我对构造函数(由设计师调用)没有影响,我需要一个不使用构造函数的解决方案(或展示如何将新对象应用于设计师或之后的类似对象)

【问题讨论】:

    标签: java vaadin vaadin7 vaadin-grid


    【解决方案1】:

    如果您可以访问网格,您可以尝试像这样定义 Integer 列:

    grid.addColumn("Column_Name", Integer.class);
    

    您必须在使用网格之前执行此操作(添加行)。

    另一种方法是使用 BeanItemContainer。来自网格的 Vadding 文档的这段代码:

    // Have some data
    Collection<Person> people = Lists.newArrayList(
        new Person("Nicolaus Copernicus", 1543),
        new Person("Galileo Galilei", 1564),
        new Person("Johannes Kepler", 1571));
    
     // Have a container of some type to contain the data
    BeanItemContainer<Person> container =
    new BeanItemContainer<Person>(Person.class, people);
    
    // Create a grid bound to the container
    Grid grid = new Grid(container);
    grid.setColumnOrder("name", "born");
    layout.addComponent(grid);
    

    更多信息: https://vaadin.com/docs/v7/framework/components/components-grid.html

    祝你好运!

    【讨论】:

    • 我会尝试第一种方法。第二个将新的 Grid 添加到某个容器中,但随后我将松散在设计器中已经完成的配置。
    • 不知道你用的是什么容器。另一种解决方案是从网格中获取容器并将列添加到容器中。也许它会起作用。定义属性(列) container.addContainerProperty("name", Integer.class, "noname");
    • 当我尝试更改列时,它们似乎已经被严格输入,所以我也得到了错误。这设计师太笨了……
    • 哈哈,好的,如果你愿意,你可以尝试让所有现有的列创建一个新容器,复制它们创建新列并将新容器设置在旧列的位置。我希望这会奏效。
    猜你喜欢
    • 2012-01-13
    • 1970-01-01
    • 2018-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多