组合框对“.setValue()”和“.select()”没有反应

Combobox doesn't react to ".setValue()" nor ".select()"

这是我的代码:

comboBoxInstance.setInputPrompt("Something...");
comboBoxInstance.setNullSelectionAllowed(false);
Cookie comboCookie = getCookieByName("combo");
comboBoxInstance.select((comboCookie != null) ? comboCookie.getValue() : null);

final TextField textFieldInstance = new TextField("Textfield");
textFieldInstance.setInputPrompt("Something...");
Cookie tfCookie = getCookieByName("tf");
textFieldInstance.setValue((tfCookie != null) ? tfCookie.getValue() : null);

问题是文本字段与 "Cookie setup" 配合得很好。只有组合框拒绝按应有的方式工作。

输出是这样的:

我试过使用 .setValue() 而不是 .select() 但这具有几乎相同的效果。我还确保提供了 Cookie 本身和正确的值。

查看生成 cookie 的部分可能会有所帮助:

Cookie comboCookie = new Cookie("combo", comboBoxInstance.getValue().toString());
cookieProcessing(costcentreCookie); //<- sets maxage and vaadin related stuff (like adding the cookie)

编辑:

数据流的几点说明。

我正在生成一个 ComboBox,其中使用 SimpleJDBCConnectionPool 的 SQLContainer 作为数据容器(来自 TableQuery)。这是组合框 class:

中的初始化(在构造函数中执行)
private void init() throws SQLException {
    this.setContainerDataSource(generateContainer());
    this.setItemCaptionPropertyId("something");
}

私有方法 generateContainer() returns 当然是SQLContainer。

如果我单击打开对话框的特定按钮,就会发生这种情况。这个对话框就是上图所示的片段。组合框 - 当然 - 是其中的一部分。

现在应该做的是设置他的数据(获取 ComboBox 的一项)并点击保存。保存按钮执行存储 cookie 的例程。这是上面已经提到的代码 (Cookie comboCookie = new Cookie(...)。

好的,现在用户将再次打开对话框。他是重新加载应用程序还是重新打开对话框(或做其他事情)并不重要。它在应用程序中基本相同。

对话框打开并再次初始化组合框(和文本字段)。然而,这次它应该从存储的 cookie 中收集数据。这是问题发生的地方。这适用于文本字段(有两个,但出于缩短的原因我省略了一个)但不适用于组合框,即使它应该具有与以前完全相同的数据。请记住,它与我们最初存储 cookie 时的初始化完全相同 class。

我有一个模糊的假设,它必须对代码的堆叠方式有所影响。也许它在尝试设置合适的值时还没有完成加载数据容器,然后找不到。


编辑2:

我终于设法揭示了一些东西。当执行“.select()”时,ComboBox 确实是空的。但是,这意味着 ComboBox 保持不变(它只是数据容器的一种 "linked"),直到有人放下项目。一旦发生这种情况,物品就在那里,我可能 select 它们。

它应该像这样工作吗? O.o 我可以在做其他事情之前完全初始化组合框吗?类似于:

private void init() throws SQLException {
    this.setContainerDataSource(generateContainer());
    this.setItemCaptionPropertyId("something");
    this.gatherTheItems();
}

Edit3 - 使用“.setImmediate(true)”进行测试

我已将 init 更改为:

private void init() throws SQLException {
    this.setContainerDataSource(generateContainer());
    this.setItemCaptionPropertyId("SOMETHING");
    this.setImmediate(true);
}

这并没有改变任何东西。组合框仍然是空的:

终于!起初我找到了这样的解决方法:

    for (Iterator it_IDS = combobox.getItemIds().iterator(); it_IDS.hasNext();) {
        Object id = (Object) it_IDS.next();
        if(id.toString().equals(cookie.getValue().toString())){
            combo2.select(id);
            break;
        }
    }

但是,我不敢相信这是有效的,因为它没有改变任何核心问题。所以我调查过,RowID 是通过 BigDecimal 和 voilà 构建的:

    if(cookie != null) {
        combobox.select(new RowId(new BigDecimal(cookie.getValue())));
    }

我现在很开心 :) 感谢您的耐心等待 kukis。

如果您来到这里是因为您在使用 BeanItemContainer 作为数据源时遇到了同样的问题,请记住您必须在基础 [=27] 上同时实现 equals()hashCode() 方法=] ComboBox 的 select()setValue() 方法起作用。

您在 Vaadin 论坛上有很多关于如何实现这些方法的示例:

ComboBox select value problem

Select or ComboBox does not Show Selected Property

Combobox select/setValue