【问题标题】:vaadin combobox item label and item valuevaadin 组合框项目标签和项目值
【发布时间】:2011-11-30 09:14:52
【问题描述】:

我是具有 JSF 背景的新 Vaadin 用户。目前我正在尝试实现一个简单的 使用vaadin。我想做的是,使用列表作为combobox的项目列表,在combobox中显示示例。description。在选定的一个项目中选择example object/example.id作为值。(AS AS)我们在 JSF 中使用 f:selectItems 的 itemLabel=example.description,itemValue=example/example.id 属性)

【问题讨论】:

  • 当前对象 ID 显示在组合框中,例如 (tw.com.tiri.City@d75415)

标签: java vaadin


【解决方案1】:

这是 Vaadin 团队的 Ville。您可以通过多种方式执行此操作,但通常您使用 setItemCaptionMode() 方法切换 ComboBox 行为。

但是,通过以下示例可以实现非常接近您想要做的事情:

public class Example {

    private Integer id;
    private String description;

    public Example(Integer id, String description) {
        this.id = id;
        this.description = description;
    }

    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getDescription() {
        return description;
    }
    public void setDescription(String description) {
        this.description = description;
    }
}

@Override
public void init() {
    Window mainWindow = new Window("Combobox Application");

    List<Example> examples = new ArrayList<Example>();
    examples.add(new Example(new Integer(1), "First description"));
    examples.add(new Example(new Integer(2), "Second description"));
    examples.add(new Example(new Integer(3), "Third description"));
    BeanItemContainer<Example> objects = new BeanItemContainer(Example.class, examples);

    ComboBox combo = new ComboBox("Example", objects);
    combo.setItemCaptionPropertyId("description");

    mainWindow.addComponent(combo);
    setMainWindow(mainWindow);
}

这里 BeanItemContainer 包装了您的 POJO 并使用反射来访问 getter。

干杯。

【讨论】:

    【解决方案2】:

    我也是 vaadin 的新手,因此无法为您提供好的解决方案,但您可以尝试为每个条目使用一些包装器对象并覆盖 toString() 方法以打印出城市名称。

    除此之外,您还可以尝试使用BeanContainer&lt;String, City&gt; 作为该组合框的数据源。该字符串将是城市名称/id。

    【讨论】:

      【解决方案3】:

      Enum 类有一种更优雅的方式:

       public enum Planet {
          MERCURY,
          VENUS,
          EARTH,
          MARS,
          JUPITER,
          SATURN,
          URANUS,
          NEPTUNE,
          PLUTO;
          public String getCaption(){
              return name();
          }
          public int getId(){
              return ordinal();
          }
      }
      
      BeanContainer<Integer, Planet> container =  new BeanContainer<>(Planet.class);
      container.setBeanIdProperty("id");
      container.addAll(EnumSet.allOf(Planet.class));
      
      ComboBox planet = new ComboBox("Planet", container);
      planet.setItemCaptionPropertyId("caption");
      

      作者: 安德里斯·拉平什

      https://vaadin.com/forum/#!/thread/280467/280466

      【讨论】:

        【解决方案4】:

        使用 vaadin 8/java 8,您可以使用:

        ComboBox combo = new ComboBox("Example", objects);

        combo.setItemCaptionGenerator(example -> example.getDescription());

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-04-05
          • 2020-08-27
          • 1970-01-01
          • 2013-03-18
          • 1970-01-01
          相关资源
          最近更新 更多